我有一個顯示Vector.Now信息的JTable,我必須定期更新表格的最後一列。爲此,我使用了一個調度程序線程並在我的主程序的構造函數中實現它。代碼運行良好,但表格不更新。它顯示我輸入的數據開始。 請幫忙。如何經常使用JAVA中的線程來更新JTable?
我的主要程序:
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);
}
}
我曾嘗試更新表,每次我更新的 「模式」。我也試過使用SwingUtilities線程,但沒有,相同的輸出沒有改變。我認爲我的矢量正在更新,但我的表格沒有更新。您可以通過查看此鏈接獲得清晰的想法:Christmas Tree in Jtable
我想要顯示在上述鏈接中的相同輸出。請仔細閱讀。經常更新的表格。
因爲如果我從數據庫中提取數據,並將其存儲在一個 JTable中說顯示it.Now如果數據庫值更改 持續那麼我的表也應顯示連續變化 給用戶的例子。這樣用戶就會知道數據在哪裏變化。
的可能的複製[?如何與數據庫填補JTable中的數據(http://stackoverflow.com/questions/2192764/how-to-fill-data-in-a -jtable-with-database) –