2017-04-20 119 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内。