JavaFx:在应用程序不同的方法执行时使用消息异步更新 UI 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19968012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFx: Update UI label asynchronously with messages while application different methods execution
提问by DHRUV BANSAL
I am trying to update the label in my JavaFx GUI asynchronously with various status message for the application.
我正在尝试使用应用程序的各种状态消息异步更新 JavaFx GUI 中的标签。
For e.g.
例如
A button 'update' in my Application call a method updateSettings() in the controller. Now I am try to update the label on the UI in the following manner.
我的应用程序中的“更新”按钮调用控制器中的方法 updateSettings()。现在我尝试以下列方式更新 UI 上的标签。
@FXML
private void updateSettings() {
label.text("message1");
//some action
lable.text("action done");
label.text("calling method.. wait for some time")
// call to time consuming method - timeConsumingMethod();
label.text
label.text("operation completely successfully");
}
private void timeConsumingMethod() {
label.text("message2");
//some actions
label.text("message3");
//more time consuming actions
label.text("time consuming method is done with success");
}
I want that these messages should be displayed in the label while the flow is getting executed, to show user about the various activities going on in the application.
我希望这些消息应该在流程执行时显示在标签中,以向用户展示应用程序中正在进行的各种活动。
How to achieve this behavior ?
如何实现这种行为?
采纳答案by jewelsea
You run your time consuming method off of the JavaFX application thread (in a Task). Tasks have special APIs in them which allow for easy provision of messages for status which can be displayed in a bound label.
您在 JavaFX 应用程序线程(在Task 中)运行耗时的方法。任务中有特殊的 API,可以轻松提供状态消息,这些消息可以显示在绑定标签中。
What I have done with the code below is try to create a system which mimics the suggested flow and message reports you provided in your question. For various reasons as documented in the code only some messages will be visible to the user.
我对下面的代码所做的是尝试创建一个系统,该系统模仿您在问题中提供的建议流程和消息报告。由于代码中记录的各种原因,用户只能看到一些消息。
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.*;
import javafx.application.*;
public class MessagingDemo extends Application {
public void start(Stage stage) {
// "message1" won't be seen because we perform the next action on the JavaFX
// application thread then update the label text again without releasing the
// thread to the JavaFX system.
Label label = new Label("message1");
label.setPrefWidth(300);
// some action
// "action done" won't be seen because we set text again in the next statement.
label.setText("action done");
// you're not going to see this because we immediately bind text to the task text and launch the task.
label.text("calling method.. wait for some time")
Task <Void> task = new Task<Void>() {
@Override public Void call() throws InterruptedException {
// "message2" time consuming method (this message will be seen).
updateMessage("message2");
// some actions
Thread.sleep(3000);
// "message3" time consuming method (this message will be seen).
updateMessage("message3");
//more time consuming actions
Thread.sleep(7000);
// this will never be actually be seen because we also set a message
// in the task::setOnSucceeded handler.
updateMessage("time consuming method is done with success");
return null;
}
};
label.textProperty().bind(task.messageProperty());
// java 8 construct, replace with java 7 code if using java 7.
task.setOnSucceeded(e -> {
label.textProperty().unbind();
// this message will be seen.
label.setText("operation completed successfully");
});
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
stage.setScene(new Scene(label));
stage.show();
}
public static void main(String args[]) {
launch(args);
}
}