JavaFX TextArea 立即更新
JavaFX TextArea Update Immediately
我正在使用 Java FX textarea 并使用它来为正在进行的步骤提供信息。
步骤如下。
复制一个文件。
删除旧文件。
复制新文件。
然后将一些属性从旧文件复制到新文件。
这整个步骤在单击按钮时开始。
我面临的问题是,当我使用追加命令时,文本区域没有立即更新。
append 命令添加数据,当函数终止时,我将所有文本放在一起。
我希望在调用函数时更新文本区域。
在我的程序中,复制文件操作需要一些时间,因为它是一个大文件。
所以在开始时我显示操作已经开始的消息。
在操作结束时我想显示操作已经结束。
但是文本区域将所有这些文本显示在一起。
我在 oracle 论坛上看到,FX 中的文本区域使用单线程,因此在整个过程完成之前不会显示任何内容。
文章:https://community.oracle.com/message/9938117#9938117
谁能建议我该怎么做?
新编辑
单击按钮确定我正在调用一个执行以下方法的函数。
public void executeCmds(){
createTempDirectory();
copyConfigPropetiesFileValues();
copyConfigProperties();
copyYMLFile();
copyYMLFileProperties();
stopTomcatServer();
deleteOldWar();
copyNewWar();
startTomcatServer();
copyOldConfigFile();
copyOldYMLFile();
}
现在每个函数都是一个进程,应该是顺序执行的。在每个步骤完成后,我想用一条成功消息更新 GUI 文本区域。
对于我使用的方法如下:
public void createTempDirectory(){
//Creating temporary directory for copying property files
status_text_area.appendText("Trying to create a temp directory \n");
File tempDir= new File(tomcat_path.getText()+filePath.path_to_temp_directory);
if(!tempDir.exists())
tempDir.mkdirs();
status_text_area.appendText("Created Temp directory to copy Config Files \n");
}
其他功能也一样。 copyWar 文件函数和删除 warfile 函数需要时间,因为它将 130 MB 文件从一个位置复制到另一个位置。
所以我希望文本区域显示为,
1.开始复制文件
一段时间后
- 文件已复制。
但问题是,在执行所有功能之前,文本区域根本不会填充。
如果我尝试通过线程执行这些,则无法保证执行顺序。
请帮忙
能否提供一些代码摘录?
我经常犯类似的错误:concat returns 连接字符串并且不修改应用了 concat 方法的字符串。
String firstString = "txt";
String resultString = firstString.concat(" next");
如果您的问题确实与线程有关,并且您的代码与您在文章中提到的代码接近,我建议您通过并行线程复制数据,例如
javafx.application.Platform.runLater(e -> {
// Do some long operation there
});
Task和runlater请参考已有文章:
Platform.runLater and Task in JavaFX
编辑:如James_D所述,如果操作真的很长,你最好使用任务。阅读我链接的文章,了解更多关于替代品及其用法的信息。
运行 后台线程中的 executeCmds()
方法并使用 Platform.runLater()
:
更新文本区域
public void executeCmds(){
Thread thread = new Thread(() -> {
createTempDirectory();
copyConfigPropetiesFileValues();
copyConfigProperties();
copyYMLFile();
copyYMLFileProperties();
stopTomcatServer();
deleteOldWar();
copyNewWar();
startTomcatServer();
copyOldConfigFile();
copyOldYMLFile();
});
thread.start();
}
然后
public void createTempDirectory(){
//Creating temporary directory for copying property files
updateStatus("Trying to create a temp directory \n");
File tempDir= new File(tomcat_path.getText()+filePath.path_to_temp_directory);
if(!tempDir.exists())
tempDir.mkdirs();
updateStatus("Created Temp directory to copy Config Files \n");
}
// similarly for other methods
private void updateStatus(String message) {
if (Platform.isFxApplicationThread()) {
status_text_area.appendText(message);
} else {
Platform.runLater(() -> status_text_area.appendText(message));
}
}
扩展已批准的响应 - 如果您需要等待更新 UI 后再做任何其他事情,请使用 PlatformImpl.runAndWait(Runnable runnable)
Thread thread = new Thread(() -> {
yourMethod();
PlatformImpl.runAndWait(() -> {
methodForUI();
});
methodAfterUIChanged();
});
thread.start();
我正在使用 Java FX textarea 并使用它来为正在进行的步骤提供信息。
步骤如下。 复制一个文件。 删除旧文件。 复制新文件。 然后将一些属性从旧文件复制到新文件。
这整个步骤在单击按钮时开始。
我面临的问题是,当我使用追加命令时,文本区域没有立即更新。
append 命令添加数据,当函数终止时,我将所有文本放在一起。 我希望在调用函数时更新文本区域。
在我的程序中,复制文件操作需要一些时间,因为它是一个大文件。 所以在开始时我显示操作已经开始的消息。 在操作结束时我想显示操作已经结束。
但是文本区域将所有这些文本显示在一起。
我在 oracle 论坛上看到,FX 中的文本区域使用单线程,因此在整个过程完成之前不会显示任何内容。
文章:https://community.oracle.com/message/9938117#9938117
谁能建议我该怎么做?
新编辑
单击按钮确定我正在调用一个执行以下方法的函数。
public void executeCmds(){
createTempDirectory();
copyConfigPropetiesFileValues();
copyConfigProperties();
copyYMLFile();
copyYMLFileProperties();
stopTomcatServer();
deleteOldWar();
copyNewWar();
startTomcatServer();
copyOldConfigFile();
copyOldYMLFile();
}
现在每个函数都是一个进程,应该是顺序执行的。在每个步骤完成后,我想用一条成功消息更新 GUI 文本区域。
对于我使用的方法如下:
public void createTempDirectory(){
//Creating temporary directory for copying property files
status_text_area.appendText("Trying to create a temp directory \n");
File tempDir= new File(tomcat_path.getText()+filePath.path_to_temp_directory);
if(!tempDir.exists())
tempDir.mkdirs();
status_text_area.appendText("Created Temp directory to copy Config Files \n");
}
其他功能也一样。 copyWar 文件函数和删除 warfile 函数需要时间,因为它将 130 MB 文件从一个位置复制到另一个位置。
所以我希望文本区域显示为, 1.开始复制文件 一段时间后
- 文件已复制。
但问题是,在执行所有功能之前,文本区域根本不会填充。
如果我尝试通过线程执行这些,则无法保证执行顺序。 请帮忙
能否提供一些代码摘录?
我经常犯类似的错误:concat returns 连接字符串并且不修改应用了 concat 方法的字符串。
String firstString = "txt";
String resultString = firstString.concat(" next");
如果您的问题确实与线程有关,并且您的代码与您在文章中提到的代码接近,我建议您通过并行线程复制数据,例如
javafx.application.Platform.runLater(e -> {
// Do some long operation there
});
Task和runlater请参考已有文章:
Platform.runLater and Task in JavaFX
编辑:如James_D所述,如果操作真的很长,你最好使用任务。阅读我链接的文章,了解更多关于替代品及其用法的信息。
运行 后台线程中的 executeCmds()
方法并使用 Platform.runLater()
:
public void executeCmds(){
Thread thread = new Thread(() -> {
createTempDirectory();
copyConfigPropetiesFileValues();
copyConfigProperties();
copyYMLFile();
copyYMLFileProperties();
stopTomcatServer();
deleteOldWar();
copyNewWar();
startTomcatServer();
copyOldConfigFile();
copyOldYMLFile();
});
thread.start();
}
然后
public void createTempDirectory(){
//Creating temporary directory for copying property files
updateStatus("Trying to create a temp directory \n");
File tempDir= new File(tomcat_path.getText()+filePath.path_to_temp_directory);
if(!tempDir.exists())
tempDir.mkdirs();
updateStatus("Created Temp directory to copy Config Files \n");
}
// similarly for other methods
private void updateStatus(String message) {
if (Platform.isFxApplicationThread()) {
status_text_area.appendText(message);
} else {
Platform.runLater(() -> status_text_area.appendText(message));
}
}
扩展已批准的响应 - 如果您需要等待更新 UI 后再做任何其他事情,请使用 PlatformImpl.runAndWait(Runnable runnable)
Thread thread = new Thread(() -> {
yourMethod();
PlatformImpl.runAndWait(() -> {
methodForUI();
});
methodAfterUIChanged();
});
thread.start();