Java JTable to be refreshed every 2 seconds

徘徊边缘 提交于 2019-12-20 06:01:20

问题


Hy! I am working on java app that has 2 forms. In first form my user order meals and I store his order in my database on localhost server. On second form that orders in JTable by using simple sql query. Now I want to make that my JTable is refreshed every 2 seconds, so I can automatically see when user order meal. I tried with Timers, but I am total newbie in Java programming so I would appreciate help... Here is my method that refreshes table but I just need a bit help about Timer:

private void NapraviTablicu() {
    dohvatiNarudzbe(); //method for executing sql queries and filling my list KuhinjaListaJela with new ordered meals
    TableModel tableModel = new KitchenTableModel(KuhinjaListaJela);//Making new table model from list
    Tablica.setModel(tableModel);// displaying new meals in table
}

回答1:


A javax.swing.Timer is a little awkward for this; as is java.util.Timer. As long as you update your TableModel on the event dispatch thread, you can query the database on another thread every two seconds using either approach cited below. Note that it's OK to sleep on a background thread.

  • SwingWorker, seen here and here.

  • Runnable, seen here.



来源:https://stackoverflow.com/questions/18725150/java-jtable-to-be-refreshed-every-2-seconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!