2017-04-20 145 views
0

執行功能,所以我有這個線程:使線程從其他類

public static class Update implements Runnable{ 

    public void run() { 
     Timer tm; 

     tm = new Timer(5000, new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent e) { 
      Clienti cl = new Clienti(); 
      cl.populateTable(); 
      } 
     }); 

    tm.start(); 

    } 
} 

,我來自另一個包(我在做這個作爲一個實驗)運行它。但是,在嘗試編程時,似乎populateTable();函數只在Update類中使用?我已經嘗試在Clienti類中創建這個類,它完美地工作,但是這似乎有一些問題。難道我做錯了什麼?

這是populateTable()方法:

public void populateTable(){ 
     MyQuery mq = new MyQuery(); 
     ArrayList<GetClass> list = mq.BindTable(); 
     String[] columnName = {"ID","Picture","Name","Age","Sex"}; 
     Object[][] rows = new Object[list.size()][5]; 
     int i; 


     for(i = 0; i < list.size(); i++){ 
      rows[i][0] = list.get(i).getId(); 

      if(list.get(i).getPicture() != null){ 

      ImageIcon image = new ImageIcon(new ImageIcon(list.get(i).getPicture()).getImage() 
      .getScaledInstance(50, 50, Image.SCALE_SMOOTH)); 

      rows[i][1] = image; 
      } 
      else{ 
       rows[i][1] = null; 
      } 

      rows[i][2] = list.get(i).getName(); 
      rows[i][3] = list.get(i).getAge(); 
      rows[i][4] = list.get(i).getSex(); 

     } 

     TheModel model = new TheModel(rows, columnName); 
     table.setModel(model); 
     table.setRowHeight(50); 
     table.getColumnModel().getColumn(4).setPreferredWidth(150); 
+0

有什麼問題? –

+0

它不更新JTable @Luminous_Dev –

+0

populateTable方法的作品?你需要給我們更多的代碼,代碼看起來不錯,我確定它的方式,你已經構建了線程,目前,這個線程不是你的GUI對話 –

回答

0

與Luminous_Dev聊天后,他想出了答案。我已將此添加到更新線程:

private Clienti clienti; 
public Update(Clienti clienti){ 
    this.clienti = clienti; 
} 

也改變

Clienti cl = new Clienti(); 
     cl.populateTable(); 

clienti.populateTable();,並增加了「本」,我創建線程的構造pharanteses內。