java - 在执行外部进程时更新我的 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");
}
解决方案
改变
tStatus.setText(line);
至
Platform.runLater(() -> tStatus.setText(line));
或者
Platform.runLater(new Runnable() {
@Override
public void run() {
tStatus.setText(line);
}
});
推荐阅读
- node.js - 需要 SOAP - Node JS 包失败并在 AWS Lambda 中出现导入错误
- excel - 范围内每个单元格的不同颜色
- angular - 禁止在 Nrwl Nx 的同一库中导入桶文件
- firebase - Firebase 数据库规则 - 基于自定义声明的规则
- javascript - 将 String.concat() 转换为模板字符串
- java - 如何在 WearOS 应用程序上直接访问互联网
- recursion - 如何优化递归球拍函数的运行时间以确定列表中元素的最大值?
- asp.net - Telerik RadTreeView 控件的添加/编辑/删除按钮
- java - 递归主方法中的 ArrayIndexOutOfBoundsException
- javascript - 如何在 Node.js 中使用引荐来源网址保护路由