首页 > 解决方案 > 在执行外部进程时更新我的​​ JavaFX 应用程序的视图

问题描述

我在执行外部进程时更新我的​​ JavaFX 应用程序的视图时遇到问题。在下面的代码中while,我放了一些代码来显示执行过程的状态。

System.out.println(line);效果很好,我在控制台中看到了进程的输出。

但是tStatus.setText(line);没有效果

两者refreshListFilesDestination()都不是

有人知道我做错了什么吗?

谢谢

ProcessBuilder builder = new ProcessBuilder(
      "cmd.exe", "/c", "java -jar \"c:\\Program Files (x86)\\JSignPdf\\JSignPdf.jar\" -kst WINDOWS-MY " + stringFilesList + " -d " + fieldDirectoryDestination.getText() + " -V");

builder.redirectErrorStream(true);
try {
    Process p = builder.start();
    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while (true) {
        line = r.readLine();
        if (line == null) {
            break;
        }
        System.out.println(line); // this work fine I see all the outpout comming in the console
        tStatus.setText(line); // Don't work, except at the very end
        refreshListFilesDestination(); // This is a method who refresh a list of files, it Don't work
    }
} catch (Exception e1) {
    tStatus.setText("Command return an error");
}

标签: javajavafxprocessbuilder

解决方案


改变

    tStatus.setText(line); 

    Platform.runLater(() -> tStatus.setText(line));

或者

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            tStatus.setText(line);
        }
    });    

查看更多关于方法 runLater()


推荐阅读