【发布时间】:2016-06-01 05:02:09
【问题描述】:
我正在使用 JavaFX,我需要以某种方式从工作线程更新 UI 元素。
我的工作线程通过套接字不断地监听传入连接。当它检测到一个时,它会调用方法 updateListOfConnections();
我想做的是 updateListOfConnections 非常简单:
this.deviceList.clear();
this.deviceList.addAll(this.contactManager.getContacts().keySet());
if(this.deviceList.size() > 0){
updateConnectButtonState(false);
} else {
updateConnectButtonState(true);
}
如您所见,我在这里影响了几个项目的 UI 状态,当我尝试从我的线程调用 updateListOfConnections 时,我得到一个异常。
我想出的一些可能的解决方案(确实不理想)是:
我只是让用户通过按下按钮调用 updateListOfConnections 手动刷新 deviceList。
或者,让一个循环不断运行,当它到达某个间隔时,重置间隔并调用 updateListOfConnections。
所以我的主要问题是如何使用 JavaFX 从 Java 中的工作线程更新 UI 元素?
【问题讨论】:
标签: java multithreading user-interface javafx