I have a JTable that displays information from a Vector.Now I have to update the last column of the table periodically. For this I took a scheduler-thread and implemented it in the constructor of my main program. The code runs fine but the Table is not updating.It is showing the data which I entered starting. Please help.
My Main Program:
public class JTableList extends JFrame
{
public static ObjectData objdata1;
static int i=0;
public JTableList()
{
VectorList.objectDataRetrive(); //vectorList is a class that first stores data into a vector of type ObjectData class.
//objectDataRetrive just stores data into vector that is in ObjectData class
final DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(VectorList.coloumn_name); //adding column names
for(int i=0;i<VectorList.size;i++)//adding row names
{
objdata1 = VectorList.vect.get(i); //VectorList returns ObjectData class object in which the data is stored already.
model.insertRow(i, new Object[] { objdata1.id , objdata1.name,objdata1.acc_no,objdata1.exchange_name,objdata1.status });
}
ScheduledExecutorService execService = Executors.newScheduledThreadPool(5);
execService.scheduleAtFixedRate(()->{
//only frequently change last column "status"
while(i<VectorList.size) //loop until size of the vector.Here it is 5
{
objdata1 = VectorList.vect.get(i);
if(objdata1.status == "Verified"){
objdata1.status = "Done";
i++;
break;
}
if(objdata1.status == "NA"){
objdata1.status = "Waiting";
i++;
break;
}
if(objdata1.status == "Pending"){
objdata1.status = "Verified";
i++;
break;
}
if(objdata1.status == "Done"){
objdata1.status = "Verified";
i++;
break;
}
if(objdata1.status == "Waiting"){
objdata1.status = "NA";
i++;
break;
}
}
if(i==VectorList.size-1)
i=0;
}, 0, 3000L, TimeUnit.MILLISECONDS);
JTable table = new JTable(model);
JTableHeader head = table.getTableHeader();
Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add("North", head);
c.add("Center",table);
}
public static void main(String[] argv)
{
JTableList jtable = new JTableList();
// JTableList.startThread();
jtable.setSize(500,400);
jtable.setVisible(true);
jtable.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I have tried updating the "model" of the table everytime I update. I tried using SwingUtilities thread also.But none, same output without changing. I think my vector is getting updated but my table is not getting updated.You can get a clear idea by seeing this link : Christmas Tree in Jtable
I want the same output that was shown in the above link. Please go through that. A table that frequently updates.
For example if I am taking data from a database and storing it in a JTable and say displaying it.Now if the database value changes continuously then my table should also DISPLAY that changes continuously to the user.So that the user will know where the data is changing.