在Java FX工作线程中不断更新UI [英] Constantly Update UI in Java FX worker thread
本文介绍了在Java FX工作线程中不断更新UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的FXML应用程序中有标签标签
。
I have Label label
in my FXML Application.
我希望此标签每秒更改一次。目前我用它:
I want this label to change once a second. Currently I use this:
Task task = new Task<Void>() {
@Override
public Void call() throws Exception {
int i = 0;
while (true) {
lbl_tokenValid.setText(""+i);
i++;
Thread.sleep(1000);
}
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
但是没有发生任何事情。
However nothing is happening.
I不要得到任何错误或例外。
我不需要我在主GUI线程中更改标签的值,所以我没有在 updateMessage
或<$ c $中看到这一点c> updateProgress 方法。
I don't get any errors or exceptions.
I don't need the value I change the label to in my main GUI thread so I don't see the point in the updateMessage
or updateProgress
methods.
有什么问题?
推荐答案
您需要在JavaFX UI线程上更改场景图。
是这样的:
you need to make changes to the scene graph on the JavaFX UI thread. like this:
Task task = new Task<Void>() {
@Override
public Void call() throws Exception {
int i = 0;
while (true) {
final int finalI = i;
Platform.runLater(new Runnable() {
@Override
public void run() {
label.setText("" + finalI);
}
});
i++;
Thread.sleep(1000);
}
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
这篇关于在Java FX工作线程中不断更新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文