Update GUI elements while in a processor consuming loop

Dear all,

I'm facing a problem since I started learning Java, about 3 years ago. There is 1 issue I haven't been able to resolve so far, but this time I really really really need to solve it. So that's why I ask for your help.

At the moment I'm creating iRipper, a program which allows you to copy music from your iPod to your harddrive with the appropriate filename (I know you have utilities on the net for this, but as far as I have found them they all require your music files to be tagged. Mine doesn't). Anyway, that's not the point.

When you've created the list of music files you want to export, you click the "export" button. A new popup will show up with a details textarea, a label which shows you how many MB's are copied from the total and a JProgressBar. BUT... here it comes.

When I've copied 1 file to the hard drive, I update the textarea, the label and the JProgressBar. Unfortunality, you cannot see this changed until java has copied ALL the files to your harddrive. THEN you see the changed. My question is: how to avoid this? I noticed that as long as Java is working on a processor consuming operation, this has a higher priority than updating GUI elements (at least, this is what I experience). Does anyone know how to solve this problem?

You can check what I mean by creating a simple JTextArea (name = taText) and execute this loop:

for(int iCounter = 0; iCounter < 200000; iCounter++)

taText.append("\nRow number " + iCounter);

So please help me with this. I've even tried to use Threads for this, but that still didn't solve my problem.

Thanks in advance!

[1749 byte] By [Nemesish3da] at [2007-11-26 23:47:38]
# 1

Dudes,

I might have found the solution.

http://java.sun.com/docs/books/tutorial/uiswing/components/examples/ProgressBarDemo.java

It says:

Task is a subclass of javax.swing.SwingWorker. The Task instance does three important things for ProgressBarDemo:

1. The instance invokes the doInBackground in a separate thread. This is where the long-running task is actually executed. Using a background thread instead of the event-dispatching thread prevents the user interface from freezing while the task is running.

2. When the background task is complete, the instance invokes the done method in the event-dispatching thread.

3. The instance maintains a bound property, progress, that is updated to indicate the progress of the task. The propertyChange method is invoked each time progress changes.

I'll try this at home and I think it should work. Sorry for opening this topic while eventually there was an answer on the net.

Kind regards,

Me

Nemesish3da at 2007-7-11 15:22:26 > top of Java-index,Java Essentials,Java Programming...