简体   繁体   English

Java不更新循环内的textarea

[英]Java dont updating textarea inside a loop

Hy. HY。

After um weak searching for this answer in a way I (java newbe) could undestand, I dedided to ask. 在以我(java newbe)可能无法理解的方式寻找这个答案之后,我决定问一下。 My program consists in a Form JFrame with a buttom that starts a routine. 我的程序包含一个带有启动程序例程的按钮的Form JFrame。

  1. JChooser to get a folder JChooser获取文件夹
  2. A list to get all files in that folder 获取该文件夹中所有文件的列表

start a for (loop) 3. A Process to unzip each of the files (.tgz). 启动for(循环)3.解压缩每个文件(.tgz)的进程。 The unfolded tgz reveal a .tar 4. A Processo to unfold the tar. 展开的tgz会显示一个.tar4。展开该tar的Processo。 5. Another Process to get the .csv file from the .tar and make some changes. 5.另一个从.tar获取.csv文件并进行一些更改的过程。 ends the for 结束

All this is inside a (private void btPegaDirActionPerformed(java.awt.event.ActionEvent evt)). 所有这些都在(私有无效的btPegaDirActionPerformed(java.awt.event.ActionEvent evt))内。 But I have a textarea (name is txtDisplay) and it is only updated after the hole process. 但是我有一个textarea(名称是txtDisplay),并且仅在打孔后才更新。 Ok, I know that Swing must have a outter process to update in real time all the time a Process is ended (ie unpiz #1, unzip #2, change CSV), and I don't know how to create the outter Process. 好的,我知道Swing必须有一个外部进程来在进程结束时一直进行实时更新(即,解开#1,解压缩#2,更改CSV),而且我不知道如何创建外部进程。

Here is the code. 这是代码。

private void btPegaDirActionPerformed(java.awt.event.ActionEvent evt) {
    JCHooser....
    Get file to array (between 28 to 32 files)

    for (File arqTGZ : files) {
        try {

            unzip tgz (unfolds a tar)
            System.out.println("Unzip OK")
            (must updata java textarea here)

            unzip tar (unfolds a csv)
            System.out.println("Unzip OK")
            (must updata java textarea here)

            change CSV
            System.out.println("CSV Saved")
            (must updata java textarea here)

        }
    }
}

That is it. 这就对了。 Could anybody help me, please? 有人可以帮我吗?

A loop is OK if it is not done on the UI/Event Dispatch Thread - if a long task is done directly on the UI/EDT thread then all rendering/interaction will cease until the operation completes making the application "freeze". 如果 UI / Event Dispatch线程上完成循环,则该循环正常- 如果直接在UI / EDT线程上完成长任务,则所有渲染/交互都将停止,直到操作完成使应用程序“冻结”为止。

Use a SwingWorker as covered in this trail to create a background thread/worker implementing a task. 使用本教程中介绍的SwingWorker来创建实现任务的后台线程/工作者 Then use bound properties and wire up the UI to listen to the appropriate status from the background worker. 然后使用绑定属性并连接UI以侦听后台工作程序的相应状态。 This approach (except for using firePropertyChange on a custom property) is covered in the SwingWorker class documentation , as PrimeNumbersTask. 这种方法(除了在自定义属性上使用firePropertyChange之外)在SwingWorker类文档中作为PrimeNumbersTask进行了介绍。

In a pinch SwingUtilities.invokeLater can be used manually to wrap the "must update" inside the background thread such that the UI operations are done on the EDT. 在紧要关头,可以手动使用SwingUtilities.invokeLater将“必须更新”包装在后台线程内,以便在EDT上完成UI操作。 However since this increases the coupling between the worker implementation and the UI, bound properties should be preferred. 但是,由于这会增加工作程序实现和UI之间的耦合,因此应首选绑定属性。

Make sure to support cancelation between activities and prevent the [accidental] execution of multiple concurrent SwingWorkers handling the same task. 确保支持活动之间的取消 ,并防止[意外]执行多个同时执行同一任务的SwingWorkers。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM