简体   繁体   English

用线程实现刷新表

[英]Implementing a refreshing table with threads

I'm doing a Swing application where in one of its forms, I have a table which I want to update periodically. 我正在做一个Swing应用程序,其中有一种形式的表我想定期更新。 I first thought of using a Timer , but the task may be time consuming and it may freeze the GUI. 我首先想到使用Timer ,但是该任务可能很耗时,并且可能会冻结GUI。 Then, I thought of using a SwingWorker , but it is designed to be executed once, and I need to execute this task periodically (every two or three minutes) while the form is open. 然后,我想到了使用SwingWorker ,但是它被设计为只能执行一次,因此我需要在打开表单时定期(每两到三分钟)执行一次此任务。

In this form's code I have implemented three methods: acceptNew() , which runs a quick UPDATE query, listRequests() , which runs some queries to fill an ArrayList with all the records; 在这种形式的代码中,我实现了三种方法: acceptNew()运行一个快速UPDATE查询, listRequests()运行一些查询以用所有记录填充ArrayList and manipulateTable() , which compares the table model with the ArrayList and updates/inserts the records. manipulateTable() ,将表模型与ArrayList进行比较,并更新/插入记录。

In short, acceptNew() does a quick task, listRequests() does another task which its completion time depends of the number of records in the DB, and manipulateTable() updates the GUI. 简而言之, acceptNew()做一个快速的任务, listRequests()做另一个任务,它的完成时间取决于数据库中的记录数, listRequests() manipulateTable()更新GUI。 I need to call these three methods periodically without freezing the GUI. 我需要在不冻结GUI的情况下定期调用这三种方法。

You can use SwingWorker's publish to emit the new state of listRequests() that you call manipulateTable() in the process method and sleep inbetween. 您可以使用SwingWorker类的publish发出的新状态listRequests()所调用manipulateTable()process方法和睡眠其间。

Or just create and submit a new SwingWorker that updates it once every time from a Timer. 或者,只需创建并提交一个新的SwingWorker,即可每次从Timer更新一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM