JavaFX TextArea立即更新 [英] JavaFX TextArea Update Immediately

查看:873
本文介绍了JavaFX TextArea立即更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java FX textarea,并使用它来提供有关正在进行的步骤的信息.

I am using Java FX textarea and using it as to provide information for the steps going on.

步骤如下. 复制文件. 删除旧文件. 复制新文件. 然后将一些属性从旧文件复制到新文件.

Steps are as follows. Copy a file. Delete old file. Copy New File. and then copy some properties from old to new file.

整个步骤从单击按钮开始.

This whole step starts when a button is clicked.

我面临的问题是,一旦使用附加命令,文本区域就不会被更新.

The problem I am facing is that text area is not being updated as soon as I use the append command.

append命令添加数据,当函数终止时,我将所有文本放在一起. 我希望在调用函数时更新文本区域.

The append command adds data and when the function terminates, I get all the text together. I want the text area to be updated as I call the function.

在我的程序中,复制文件操作需要一些时间,因为它是一个大文件. 因此,在开始时我会显示该操作已开始的消息. 在操作结束时,我要显示的操作已结束.

In my program, the copy file operation, takes some time as it is a big file. So in the start I display message that the operation has started. and at the end of operation I want to display the operation has ended.

但是文本区域会同时显示所有这些文本.

But the text area displays all these texts all together.

我在oracle论坛中读到,FX中的文本区域使用单个线程,因此在整个过程完成之前不会显示任何内容.

I read in oracle forum, that text area in FX uses a single thread hence wont display anything until the whole process is complete.

文章: https://community.oracle.com/message/9938117#9938117

谁能建议我该怎么办??

Can any one suggest what should I do.?

新编辑

在按钮上单击确定,我正在调用一个函数,该函数将执行以下方法.

Okay on Button Click I am calling a function which executes the following methods.

  public void executeCmds(){

        createTempDirectory();
        copyConfigPropetiesFileValues();
        copyConfigProperties();
        copyYMLFile();
        copyYMLFileProperties();

        stopTomcatServer();

        deleteOldWar();
        copyNewWar();
        startTomcatServer();

        copyOldConfigFile();
        copyOldYMLFile();
 }

现在,每个功能都是一个过程,应按顺序执行.并且在完成每个步骤之后,我想用一个成功的消息来更新GUI文本区域,该消息已完成.

Now Each of the function is a process, which should be executed sequentially. And after each step is completed I want to update the GUI text area with a success message that this is complete.

对于我正在使用的方法如下:

For the I am using method as follows:

  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文件从一个位置复制到另一个位置.

and same is with other functions. The copyWar file function and delete warfile function take time as it copies 130 MB file from a location to other.

所以我希望将文本区域显示为 1.开始复制文件 一段时间后

So I want the textarea to be displayed as, 1. Started copying file and after some time

  1. 已复制文件.

但是问题是,在执行所有功能之前,文本区域根本不会填充.

But the issue is , the text area does not populate at all, until all the functions are executed.

如果我尝试通过线程执行这些操作,则执行顺序不予保证. 请帮助

If I try to execute these via threads, then the order of execution is not guranteed. Please Help

推荐答案

在后台线程中运行executeCmds()方法,并使用Platform.runLater()更新文本区域:

Run your executeCmds() method in a background thread and update the text area using 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));
    }
}

这篇关于JavaFX TextArea立即更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆