2017-02-24 36 views
0

我有一個顯示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如果數據庫值更改 持續那麼我的表也應顯示連續變化 給用戶的例子。這樣用戶就會知道數據在哪裏變化。

+0

的可能的複製[?如何與數據庫填補JTable中的數據(http://stackoverflow.com/questions/2192764/how-to-fill-data-in-a -jtable-with-database) –

回答

3

我覺得我的矢量得到更新

忘掉矢量。 Vector僅用於將數據加載到JTableTableModel中。數據加載後,您可以通過TableModel訪問表格的數據。

不要更新您的矢量。

相反,你在TableModel更新數據:然後

table.getModel.setValueAt(...); 

表將自動重繪自己。

不要遍歷Vector。同樣,你將通過TableModel使用迭代:

String value = table.getValueAt(...).toString(); 

if (value.equals(...)) 
    // do something 

另外,不要:

  1. 使用魔法值。人們不知道那些String是什麼意思。該API將有一個可用作約束的變量。
  2. 使用add(...)方法的形式。閱讀API,你會看到你應該使用該方法的新形式。

因此,代碼應該是:

//c.add("North", head); 
    //c.add("Center",table); 
    c.add(head, BorderLayout.PAGE_START); 
    c.add(table, BorderLayout.CENTER); 

當使用JTable中,你會表通常添加到滾動窗格,而不是自己管理的頭和表。所以,你可以使用:

//c.add(head, BorderLayout.PAGE_START); 
//c.add(table, BorderLayout.CENTER); 
JScrollPane scrollPane = new JScrollPane(table); 
c.add(scrollPane, BorderLayout.CENTER) 
2

我敢肯定你的問題是,GUI的變化總是由AWT線程中完成的,而不是由任何其他創建的線程(而你也有你的代碼的一些其他小的缺陷)

你可以使用「SwingUtilities.invokeAndWait(Runnable)」來強制GUI線程正在執行給定的Runnable並等待它的執行。或者,如果您不需要等待,你也可以使用「SwingUtilities.invokeLater(Runnable接口),而不是哪一個更好取決於您的需求

在你的榜樣它會是什麼樣子:!

 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 
      { 
       SwingUtilities.invokeAndWait(new Runnable() { 
        @Override 
        public void run() { 
          objdata1 = VectorList.vect.get(i); 
          if(objdata1.status.equals("Verified")){ 
           objdata1.status = "Done"; 
           model.fireTableDataChanged(); 
           i++; 
           break; 
         } 
        } 
      //... 

       } 
      } 
      if(i==VectorList.size-1) 
       i=0; 
     }, 0, 3000L, TimeUnit.MILLISECONDS);  
3

改變數據後,您需要通知更改的數據表模型,您可以更新objdata1.status

model.fireTableDataChanged(); 
+0

即使我添加行相同的事情發生。沒有改變。表中的輸出必須像股市圖一樣週期性地改變圖 –

2

後使用更改事件 數據可以插入:

final int finalI = i; 
SwingUtilities.invokeLater(new Runnable() { 
    public void run() { 
    model.fireTableCellUpdated(finalI, 4); 
    } 
} 

的,而塊

[編輯],測試與字符串相等後等於代替==

+0

同樣的事情兄弟。輸出中沒有任何變化。它需要像機場的航班狀態板一樣更改。 –

+0

呃。 String == Lity ... –

+0

哼,你試過用'.equals()'替換'=='來測試嗎? –