我想有效地对JavaFX TableView进行“轮询”,以便如果另一个用户在数据库中创建了一个作业,则当前用户会选择该作业(比方说每5秒钟一次)。
我已经尝试过使用计时器;
new Timer().schedule(new TimerTask() {
@Override
public void run() {
try {
newI(connection, finalQuery, adminID);
} catch (SQLException e) {
e.printStackTrace();
}
}
}, 0, 5000);
但是,这会出现以下错误:我认为
Exception in thread "Timer-0" java.lang.IllegalStateException: This operation is permitted on the event thread only; currentThread = Timer-0
表示JavaFX不支持它?我如何能够定期更新JavaFX中的TableView? 最佳答案
您可以使用ScehduleService-这样的东西...
private class MyTimerService extends ScheduledService<Collection<MyDTO>> {
@Override
protected Task<Collection<MyDTO>> createTask() {
return new Task<Collection<MyDTO>>() {
@Override
protected Collection<MyDTO> call() throws ClientProtocolException, IOException {
//Do your work here to build the collection (or what ever DTO).
return yourCollection;
}
};
}
}
//Instead of time in your code above, set up your schedule and repeat period.
service = new MyTimerService () ;
//How long the repeat is
service.setPeriod(Duration.seconds(5));
//How long the initial wait is
service.setDelay(Duration.seconds(5));
service.setOnSucceeded(event -> Platform.runLater(() -> {
//where items are the details in your table
items = service.getValue();
}));
//start the service
service.start();
关于java - 定期刷新JavaFX TableView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32481466/