我正在使用Java FX textarea,并将其用作后续步骤的信息。
步骤如下。复制文件。删除旧文件。复制新文件。然后将一些属性从旧文件复制到新文件。
单击按钮将开始整个步骤。
我面临的问题是,一旦使用append命令,文本区域就不会被更新。
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文件功能和delete warfile功能需要一段时间,因为它将130 MB文件从一个位置复制到另一个位置。
因此,我希望将textarea显示为:1.开始复制文件,过一会儿
但是问题是,在执行所有功能之前,文本区域根本不会填充。
如果我尝试通过线程执行这些操作,那么执行顺序就不予保证。请帮忙
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));
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句