I have a JFrame
with a CardLayout
set as its layout manager. This has two JPanel
subclasses in it. One is a panel, WordsLoadingPanel
, which displays the text "Loading words..." and has a JProgressBar
. The other has to actually load the words. This takes a while (about 10-14 seconds for 100 words; it's a pretty selective algorithm), so I want to assure the user that the program is still working. I made the loading algorithm in the panel fire a property change with firePropertyChange(String, int, int)
, and the WordsLoadingPanel
is catching the change just fine - I know this because I added a listener for this event to perform a println
, and it works. However, when I change the println
to actually changing the JProgressBar
's value, it doesn't do anything. I know I'm changing the value right, because if I set the value before the algorithm starts, it works, and it works on the last iteration of the loop. I'm guessing this is because my algorithm is eating the computing power and won't let JProgressBar
update.
So, my question is: How do I make my algorithm wait for Swing (would this be the AWT Dispatching Thread?) to finish updating the progress bar before continuing? I've tried:
Thread.yield
in each iteration of the loopThread.sleep(1000L)
in each iteration of the loop, in a try/catch- putting everything in the loop in a
SwingUtilities.invokeLater(Runnable)
- putting only the CPU-intensive algorithm in a
SwingUtilities.invokeLater(Runnable)
EDIT: To further support my hypothesis of the CPU-eating algorithm (sounds like a children's story…), when I set the JProgressBar
to indeterminate, it only starts moving after the algorithm finishes.
Does anyone have any suggestions?
Thanks!