繁体   English   中英

定期刷新JavaFX TableView

[英]Periodically Refresh JavaFX TableView

我想有效地对JavaFX TableView进行“轮询”,以便如果另一个用户在数据库中创建了一个作业,则当前用户会选择该作业(比方说每5秒钟一次)。

我已经尝试过使用Timer;

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 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();

暂无
暂无

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

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