Dennis
Dennis

Reputation: 3596

How to Update Swing JProgressBar in Java

I'm really new to Java, especially working with GUI in Java. I want to put a progress bar in my program, where it updates its value each time after certain amount of work has been done. Right now I have a button that executes a method when pressed, and I put setValue() function (with the value that I want) each time after certain work has been done in that method. However, when I press the button, the button seems to be stuck pressed while the method continues (the method takes a while to finish executing), and the progress bar doesn't get updated until all the other things has been done in the ButtonListener, so it goes directly to 100% after it's done).

What am I doing wrong?

Thanks in advance.

Upvotes: 0

Views: 494

Answers (1)

Harry Joy
Harry Joy

Reputation: 59660

That is, most probably, because you are doing all work in main thread so it hangs your UI until all work is done. You should not do this. You should use separate EDT for such operations. What you should try is do your update progressBar job in separate EDT. Use SwingUtilities.invokeLater(Runnable r) for this.

Upvotes: 3

Related Questions