java 每两秒刷新一次,Java JTable每2秒刷新一次

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

}

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值