定期刷新JavaFX表视图
我希望在JavaFXTableView上进行有效的“轮询”,这样,如果另一个用户在数据库中创建了一个作业,那么当前用户就会选择它(假设每5秒一次) 我试过使用定时器定期刷新JavaFX表视图,java,javafx,tableview,Java,Javafx,Tableview,我希望在JavaFXTableView上进行有效的“轮询”,这样,如果另一个用户在数据库中创建了一个作业,那么当前用户就会选择它(假设每5秒一次) 我试过使用定时器 new Timer().schedule(new TimerTask() { @Override public void run() { try { newI(connection, finalQuery, adminID); } catch (SQLExcep
new Timer().schedule(new TimerTask() {
@Override
public void run() {
try {
newI(connection, finalQuery, adminID);
} catch (SQLException e) {
e.printStackTrace();
}
}
}, 0, 5000);
但是,这会得到以下错误:
线程“Timer-0”java.lang.IllegalStateException中的异常:仅允许在事件线程上执行此操作;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();
私有类MyTimerService扩展ScheduledService{
@凌驾
受保护的任务createTask(){
返回新任务(){
@凌驾
受保护的集合调用()引发ClientProtocolException,IOException{
//在这里完成您的工作以构建集合(或任何DTO)。
归还你的藏品;
}
};
}
}
//不要在上面的代码中使用时间,而是设置您的时间表和重复周期。
服务=新的MyTimerService();
//重复多长时间
服务。设置周期(持续时间。秒(5));
//最初的等待时间是多长
服务。设置延迟(持续时间。秒(5));
service.setonSucceed(事件->平台.runLater(()->{
//表中的详细信息在哪里
items=service.getValue();
}));
//启动服务
service.start();
您可以使用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();
私有类MyTimerService扩展ScheduledService{
@凌驾
受保护的任务createTask(){
返回新任务(){
@凌驾
受保护的集合调用()引发ClientProtocolException,IOException{
//在这里完成您的工作以构建集合(或任何DTO)。
归还你的藏品;
}
};
}
}
//不要在上面的代码中使用时间,而是设置您的时间表和重复周期。
服务=新的MyTimerService();
//重复多长时间
服务。设置周期(持续时间。秒(5));
//最初的等待时间是多长
服务。设置延迟(持续时间。秒(5));
service.setonSucceed(事件->平台.runLater(()->{
//表中的详细信息在哪里
items=service.getValue();
}));
//启动服务
service.start();
您可以使用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();
私有类MyTimerService扩展ScheduledService{
@凌驾
受保护的任务createTask(){
返回新任务(){
@凌驾
受保护的集合调用()引发ClientProtocolException,IOException{
//在这里完成您的工作以构建集合(或任何DTO)。
归还你的藏品;
}
};
}
}
//不要在上面的代码中使用时间,而是设置您的时间表和重复周期。
服务=新的MyTimerService();
//重复多长时间
服务。设置周期(持续时间。秒(5));
//最初的等待时间是多长
服务。设置延迟(持续时间。秒(5));
service.setonSucceed(事件->平台.runLater(()->{
//表中的详细信息在哪里
items=service.getValue();
}));
//启动服务
service.start();
您可以使用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();
私有类MyTimerService扩展ScheduledService{
@凌驾
受保护的任务createTask(){
返回新任务(){
@凌驾
受保护的集合调用()引发ClientProtocolException,IOException{
//在这里完成您的工作以构建集合(或任何DTO)。
归还你的藏品;
}
};
}
}
//不要在上面的代码中使用时间,而是设置您的时间表和重复周期。
服务=新的MyTimerService();
//重复多长时间
服务。设置周期(持续时间。秒(5));
//最初的等待时间是多长
服务。设置延迟(持续时间。秒(5));
service.setonSucceed(事件->平台.runLater(()->{
//表中的详细信息在哪里
items=service.getValue();
}));
//启动服务
service.start();
这并不意味着JavaFX不支持它。这意味着您正在从后台线程而不是从FX应用程序线程更新某些内容。考虑使用一个代替定时器,并读取多线程和JavaFX(例如或或许多其他源),这并不意味着它在JavaFX中不被支持。这意味着您正在从后台线程而不是从FX应用程序线程更新某些内容。考虑使用一个代替定时器,并读取多线程和JavaFX(例如或或许多其他源),这并不意味着它在JavaFX中不被支持。这意味着您正在从后台线程而不是从FX应用程序线程更新某些内容。考虑使用一个代替定时器,并读取多线程和JavaFX(例如或或许多其他源),这并不意味着它在JavaFX中不被支持。这意味着您正在从后台线程而不是从FX应用程序线程更新某些内容。考虑使用一个代替定时器,并在多线程和JavaFX上读取(例如或或许多其他源),尽管您可能想用一个真正的返回类型替换<代码>空隙,并使用<代码>调用< /Cord>方法返回数据库查询的结果(例如,<代码>列表< /代码>)@James_D你知道为什么会这样吗?虽然您可能希望用实际返回类型替换Void
,并让调用
方法返回数据库查询的结果(例如列表
)。@James\D您知道为什么会发生这种情况吗?虽然您可能希望用实际返回类型替换Void
,并且