首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaFx -更新图形用户界面

JavaFx -更新图形用户界面
EN

Stack Overflow用户
提问于 2014-10-25 03:18:53
回答 1查看 27.3K关注 0票数 11

我只想在我的程序运行时更新一个标签。我正在读取一些文件,我想让它显示正在读取的文件的名称。

但是,它只使用下面的代码显示最后一个文件(基本上,GUI在整个过程完成之前不会响应):

代码语言:javascript
复制
static Text m_status_update = new Text(); //I declared this outside the function so dont worry
m_status_update.setText("Currently reading " + file.getName());

我有大约4-5个文件,我只想显示名称。

我看到了一个类似的问题Displaying changing values in JavaFx Label,最佳答案推荐如下:

代码语言:javascript
复制
Label myLabel = new Label("Start"); //I declared this outside the function so dont worry
myLabel.textProperty().bind(valueProperty);

但是,valueProperty是一个StringProperty,我不得不将字符串转换为字符串属性。

另外,我看到了这个Refresh label in JAVAFX,但是OP可以根据操作更新标签。我真的没有任何行动吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-25 03:51:22

如果您在FX应用程序线程上运行整个进程,那么该线程(实际上)就是用于显示UI的同一个线程。如果UI的显示和文件迭代过程都在同一线程中运行,则一次只能发生一种情况。因此,在该过程完成之前,您会阻止UI更新。

这里有一个简单的例子,我只是在每次迭代之间暂停250毫秒(模拟读取一个相当大的文件)。一个按钮就可以在FX应用程序线程中启动它(注意,当它运行时,UI是如何无响应的-你不能在文本字段中键入)。另一个按钮使用Task在后台运行它,正确地安排对FX应用程序线程上的UI的更新。

代码语言:javascript
复制
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class UpdateTaskDemo extends Application {

    @Override
    public void start(Stage primaryStage) {
        Label label = new Label();
        Button runOnFXThreadButton = new Button("Update on FX Thread");
        Button runInTaskButton = new Button("Update in background Task");
        HBox buttons = new HBox(10, runOnFXThreadButton, runInTaskButton);
        buttons.setPadding(new Insets(10));
        VBox root = new VBox(10, label, buttons, new TextField());
        root.setPadding(new Insets(10));

        runOnFXThreadButton.setOnAction(event -> {
            for (int i=1; i<=10; i++) {
                label.setText("Count: "+i);
                try {
                    Thread.sleep(250);
                } catch (InterruptedException exc) {
                    throw new Error("Unexpected interruption");
                }
            }

        });

        runInTaskButton.setOnAction(event -> {
            Task<Void> task = new Task<Void>() {
                @Override 
                public Void call() throws Exception {
                    for (int i=1; i<=10; i++) {
                        updateMessage("Count: "+i);
                        Thread.sleep(250);
                    }
                    return null ;
                }
            };
            task.messageProperty().addListener((obs, oldMessage, newMessage) -> label.setText(newMessage));
            new Thread(task).start();
        });

        primaryStage.setScene(new Scene(root, 400, 225));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26554814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档