[英]Java Swing - Refresh jTable using Timer
I have 2 simple database query JTable
(Client and Server). 我有2个简单的数据库查询
JTable
(客户端和服务器)。
Client has all functions like view records, print, save PDF etc. Server auto refresh database records to table with a timer of 30secs. 客户端具有查看记录,打印,保存PDF等所有功能。服务器自动刷新数据库记录到表,计时器为30秒。 (Not yet implemented.)
(尚未实现。)
My problem is I can display the database records to table with no issue with the following code. 我的问题是我可以将数据库记录显示到表而没有以下代码的问题。
PreparedStatement pst = conn.prepareStatement("SQL");
ResultSet rs = pst.ExecuteQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
But I wish to implement the auto refreshing of table with the above code with a timer. 但我希望用上面的代码用计时器实现表的自动刷新。
Example, I inserted the codes into a methods called public void Update_Records()
. 例如,我将代码插入名为public void
Update_Records()
。 How am I supposed to use the timer to call the method to display the records into the table every 30 secs? 我该如何使用计时器调用方法每隔30秒将记录显示到表中?
You could do: 你可以这样做:
Timer timer = new Timer(0, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateRecords();
}
});
timer.setDelay(30000); // delay for 30 seconds
timer.start();
Aside: Java naming conventions have methods starting with lowercase and underscores are generally not used so Update_Records
becomes updateRecords
. 另外: Java命名约定通常不使用以小写和下划线开头的方法,因此
Update_Records
变为updateRecords
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.