JavaFx的:与异步的消息更新UI的标签,而应用不同的方法执行 [英] JavaFx: Update UI label asynchronously with messages while application different methods execution

查看:3031
本文介绍了JavaFx的:与异步的消息更新UI的标签,而应用不同的方法执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的JavaFx的图形用户界面与应用程序的各种状态信息来更新标签是异步的。

I am trying to update the label in my JavaFx GUI asynchronously with various status message for the application.

有关例如

在我的应用程序的按钮更新呼叫在控制器的方法updateSettings()。现在我尝试更新UI上的标签以下列方式。

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.

@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.

如何实现这种行为?

推荐答案

您运行耗时的方法关闭JavaFX应用程序线程(在一个的Task )。任务对他们有特殊的API,允许容易提供可显示在一个开往标签状态信息。

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.

我曾与code做如下尝试创建它模仿建议的流动和信息报告,你在你的问题提供了一种系统。对于记载于code种种原因只有一些消息将是用户可见。

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

这篇关于JavaFx的:与异步的消息更新UI的标签,而应用不同的方法执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆