-3

I am having trouble on repopulating my table every time please() gets called.

It keeps adding to the existing rows and that's not what I want.

I want to repopulate the table base off my model2. Can you someone help me on recreating the table every time please() gets called. My error is where the model2.addRow(please) is called...

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;


public class TableTimeChange {

    String rowdata[] = new String[8];

    Date[] date123 = new Date[8];
    Date[] date1234 = new Date[8];

    String[] ddd = new String[8];

    static Calendar time1 = Calendar.getInstance();
    static Calendar time2 = Calendar.getInstance();

    Calendar time3[] = new Calendar[8];

    Calendar cal8 = Calendar.getInstance();

    Date date = new Date();

    DefaultTableModel model2 = new DefaultTableModel();

    static int count = 0;
    static boolean state = true;

    private SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
    private JTable table = new JTable(getTableModel());

    public TableTimeChange() {
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JFrame frame = new JFrame();
        frame.add(new JScrollPane(table));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();

        Timer timer = new Timer(1000, new TimerListener());
        timer.start();
        frame.setVisible(true);

        Calendar cal1 = new GregorianCalendar();

        cal1.add(Calendar.SECOND, 5);

        date = cal1.getTime();

        cal8.setTime(date);

    }

    private TableModel getTableModel() {
        String[] cols = {"Time", "Is Seconds Even", "tttt"};
        Object[][] data = {{getNewRow()}, {getNewRow()}};
        DefaultTableModel model = new DefaultTableModel(data, cols);
        return model;
    }

    private String getFormatDate(Date date) {
        return format.format(date);
    }

    private Object[] getNewRow() {
        Calendar cal = Calendar.getInstance();
        int seconds = cal.get(Calendar.SECOND);
        boolean isSecondsEven = (seconds % 2 == 0);
        return new Object[]{getFormatDate(cal.getTime()), isSecondsEven};
    }

    public void please() {

        while (state == true) {

            System.out.println(model2.getRowCount());

            Calendar cal = new GregorianCalendar();
            cal.add(Calendar.HOUR_OF_DAY, - 5);

            Calendar cal2 = new GregorianCalendar();
            cal2.add(Calendar.HOUR_OF_DAY, 7);

            Date date2 = cal.getTime();
            Date date3 = cal2.getTime();

            Date date1 = new Date();

            SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss a");

            Date d1[] = new Date[8];

            DateFormat sdf = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a");
            DateFormat sdf2 = new SimpleDateFormat("hh:mm:ss a");

            String rod = sdf2.format(date2);
            String rod1 = sdf2.format(date3);

            Calendar time1 = Calendar.getInstance();
            Calendar time2 = Calendar.getInstance();

            try {
                time1.setTime(sdf2.parse(rod));
                time2.setTime(sdf2.parse(rod1));

                time1.setTimeZone(TimeZone.getTimeZone("MDT"));
                time2.setTimeZone(TimeZone.getTimeZone("MDT"));

            } catch (ParseException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }

            Vector<String> please = new Vector<String>();

            for (int i = 0; i < 8; i++) {
                try {
                    d1[i] = sdf.parse(rowdata[i]);
                    ddd[i] = sdf2.format(d1[i]);
                    time3[i] = Calendar.getInstance();
                    time3[i].setTime(sdf2.parse(ddd[i]));
                    time3[i].setTimeZone(TimeZone.getTimeZone("MDT"));

                    count++;

                    if (time3[i].after(time1) && time3[i].before(time2)) {
                        Vector please2 = new Vector();
                        String exp = time3[i].getTime().toString();
                        Date ddddd = time3[i].getTime();
                        String dddd = format.format(ddddd);
                        please2.add(dddd);
                        model2.addRow(please2);
                    }
                } catch (ParseException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

            if (count == 8) {
                state = false;
            }
        }
    }

    public void remove() {
        for (int i = 0; i < model2.getRowCount(); i++) {
            model2.removeRow(i);
        }
    }

    private class TimerListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            DefaultTableModel model = (DefaultTableModel) table.getModel();

            model2 = model;

            String array1[] = {"08-10-2014 08:26:00 PM", "12-15-2001 10:18:54 PM"};
            String array2[] = {"08-19-2014 01:18:27 AM", "01-19-2019 01:28:36 PM"};
            String array3[] = {"08-05-2014 05:18:57 PM", "08-25-1989 09:18:27 PM"};
            String array4[] = {"08-15-2014 08:23:35 PM", "10-25-1985 06:48:27 AM"};
            String array5[] = {"08-10-2014 06:00:00 PM", "05-16-2011 03:29:10 AM"};
            String array6[] = {"08-25-2014 09:14:08 PM", "11-22-2010 07:00:56 PM"};
            String array7[] = {"08-26-2014 09:09:17 PM", "02-05-1969 01:18:17 AM"};
            String array8[] = {"08-19-2014 10:02:15 PM", "12-09-2010 05:09:10 PM"};
            String[] p = {"L", "S", "R", "T", "D", "C", "L", "R"};
            String[] names = {"john", "doe", "moses", "roddrick", "pgmm", "pop", "Don", "Sandia"};

            boolean[] t3;

            int count2 = 0;

            rowdata[0] = array1[0];
            rowdata[1] = array2[0];
            rowdata[2] = array3[0];
            rowdata[3] = array4[0];
            rowdata[4] = array5[0];
            rowdata[5] = array6[0];
            rowdata[6] = array7[0];
            rowdata[7] = array8[0];

            please();

            Date pop = new Date();

            System.out.println(format.format(pop));
            System.out.println(format.format(date));

            if (format.format(pop).equals(format.format(date))) {
                Calendar cal1 = new GregorianCalendar();
                cal1.add(Calendar.SECOND, 5);
                date = cal1.getTime();
                System.out.println("My Lord");
                state = true;
                count = 0;
                remove();
                please();
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TableTimeChange();
            }
        });
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
ddd
  • 1
  • 4
    The `while-loop` looks troubling... – MadProgrammer Aug 26 '14 at 22:36
  • The while loop is fine. MY table is not recreating every time the please function is called. It keeps adding and adding – ddd Aug 26 '14 at 22:37
  • I thought it might be blocking the EDT, it was the first thing that jumped out at me. In your `please` method, create a new instance of `model2`, fill it and then apply it to the table using `JTable#setModel` – MadProgrammer Aug 26 '14 at 22:40
  • 3
    @ddd: the while loop is anything but "fine". You're stepping all over the Swing event thread. Don't assume that anything is fine until you know where your bug is coming from, else you're hampering your ability to progress. – Hovercraft Full Of Eels Aug 26 '14 at 22:40
  • would you mind showing me @MadProgrammer – ddd Aug 26 '14 at 22:41
  • 1
    The `remove` method is also wrong, as it won't possibly be able to delete all the rows, instead you should be using `while (model2.getRowCount() != 0) { model2.remove(0); }` – MadProgrammer Aug 26 '14 at 22:41
  • 2
    In your `while-loop`, the `count` value is not reset before the loop begins which means that it's possible, if you're not very careful, for the loop to end up in a infinite state. You should be using `while (count < 8) {...}` or some such to guard against it... – MadProgrammer Aug 26 '14 at 22:43
  • how would my table re populate @MadProgrammer – ddd Aug 26 '14 at 22:49

1 Answers1

1

In your please method, create a new instance of TableModel, fill it, and apply it to your JTable, for example...

public void please() {

    String[] cols = {"Time", "Is Seconds Even", "tttt"};
    model2 = new DefaultTableModel(cols, 0);
    while (state == true) {
        //...
    }

    table.setModel(model2);
}

This is dangerous...

while (state == true) {
    //..
    if (count == 8) {
        state = false;
    }
}

You are relying on the state of an external value, but you're not checking it before you start you loop, what happens if the count is 9?

Instead you should be doing something more like...

while (count < 9) {
    //..
}

Or have the count value passed into the method or reset it yourself internally

Your remove method is also wrong...

public void remove() {
    for (int i = 0; i < model2.getRowCount(); i++) {
        model2.removeRow(i);
    }
}

You should not be moving i as i may no longer exist

  • rowCount = 5, remove 0
  • rowCount = 4, remove 1
  • rowCount = 3, remove 2
  • rowCount = 2, remove 3... problem here...

Instead, you should always remove the first element

public void remove() {
    while (model2.getRowCount() > 0) {
        model2.removeRow(0);
    }
}

Alternativly, because you're using a DefaultTableModel, you could just do model2.setRowCount(0);

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • when I call table.setModel(model2) nothing is showing in the frame – ddd Aug 26 '14 at 22:53
  • I provided the code for you. Would you mind modifying it – ddd Aug 26 '14 at 22:59
  • 1
    Actually, yes I would mind. I'm not a coding service, nor a debugging service. There is a logic error in your `please` method which allowing bracketing the time values (`time3`) outside of the range of `time1` and `time2`. Because I don't know what it is you are trying to achieve or why, it's impossible for more to make recommendations. Take this as an oppurtunity to flex your debugging skills. Add some `System.out.println` statements in to see what the values are and even run a debugger over it. The basic requirements to improve your code are spelled out above... – MadProgrammer Aug 27 '14 at 00:13
  • @MadProgrammer such ppl doesnt deserve your time and precious advise. This person just post qns assuming people are hired for fixing his issues. – spiderman Aug 28 '14 at 14:42