0

Hy.

After um weak searching for this answer in a way I (java newbe) could undestand, I dedided to ask. My program consists in a Form JFrame with a buttom that starts a routine.

  1. JChooser to get a folder
  2. A list to get all files in that folder

start a for (loop) 3. A Process to unzip each of the files (.tgz). The unfolded tgz reveal a .tar 4. A Processo to unfold the tar. 5. Another Process to get the .csv file from the .tar and make some changes. ends the for

All this is inside a (private void btPegaDirActionPerformed(java.awt.event.ActionEvent evt)). But I have a textarea (name is txtDisplay) and it is only updated after the hole process. Ok, I know that Swing must have a outter process to update in real time all the time a Process is ended (i.e. unpiz #1, unzip #2, change CSV), and I don't know how to create the outter Process.

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?

  • I think you need to break your program (and your question) into smaller chunks. Try to just write the bit that "updates the CSV", with out worrying about a text area or the tar file. Then ask question when you have difficulties with that. – markspace Sep 15 '14 at 18:54
  • http://stackoverflow.com/questions/629315/dynamically-refresh-jtextarea-as-processing-occurs – mika Sep 15 '14 at 18:54

1 Answers1

1

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".

Use a SwingWorker as covered in this trail to create a background thread/worker implementing a task. Then use bound properties and wire up the UI to listen to the appropriate status from the background worker. This approach (except for using firePropertyChange on a custom property) is covered in the SwingWorker class documentation, as 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. However since this increases the coupling between the worker implementation and the UI, bound properties should be preferred.

Make sure to support cancelation between activities and prevent the [accidental] execution of multiple concurrent SwingWorkers handling the same task.

user2864740
  • 60,010
  • 15
  • 145
  • 220