1

I realize that without code this might be hard to answer but the problem is I am not sure what code to post. My problem is that when I load an XML file, unmarshal it and then iterate a ArrayList of values loaded in to repopulate a JLabel array that get placed on a JPanel inside a JScrollPane, the graphics do not update until the entire processing is done. I discovered this when I created a JFrame that opened up with a JProgressBar. The bar does not even show or paint on the pane Frame until everything is done. This defeats the purpose since I am trying to have the progress bar show the progress of the decoding. While debugging that, I discovered that if I isolated the code (except for actually loading the file and doing the unmarshalling) the bar worked. That got me looking and I noticed that the other components mentioned were not updating when the individual JLabels were added and sized until all process was complete despite constant repaints. Since I am still new to JAVA I thought I would ask if anyone might know a reason that all of these components would not be graphically updating while this loop is going on.

As a note, in the loop, I call the add and repaint functions to place the newly decoded JLabel on the pane but it does not show until all is complete just like the bar. And all of this is running single thread (except for the JProgressBar which is spun into it's own thread).ll

I can post code if you like just please direct me to which code you want (IE the XML decoder and loop, the add and size of the JLabels, the instantiations, etc).

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Patrick Aquilone
  • 584
  • 2
  • 11
  • 28

2 Answers2

2

You are doing your work on the "event dispatch" thread, which is the same thread used to run swing. You need to do your work in a separate thread. Take a look at the SwingWorker class

black panda
  • 2,842
  • 1
  • 20
  • 28
  • Ok, I have this concept on my JProgressBar implementation (code at http://stackoverflow.com/questions/10450571/how-can-i-make-a-progress-bar-class-thats-value-can-be-updated-from-the-calling towards bottom), my question is where should this be? I am guessing in the class that "adds" the JLabels on the pane or would it be where I instantiate the JPanel. Again, I am new and learning and appreciate the help. – Patrick Aquilone May 05 '12 at 18:22
  • @JesterHawk I'll add an example to your original question that may be of use. – black panda May 05 '12 at 22:46
2

Your problem is that you're doing all processing on the Swing event thread or EDT. The solution is to use a background thread such as can be obtained by a SwingWorker object.

For more on this, please check out the Java Swing tutorial called Concurrency in Swing. It will describe the event thread, why it is important to respect and not to block, and how to do background processing with a SwingWorker object.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373