Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Force JProgressBar to Refresh  RSS feed

 
Charles Mulloy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand it, if I give a program a long sequence of tasks, the JProgressBar will not update until it finishes. In my program there is a method that is executed a certain number of times in a row. I want the bar to update every time the method is run. How can I force the UI to update whenever the method is called? I've thought about using threads, but I was taught to use them in separate classes. If I do this then, I can't let one thread "go to town" and simultaneously update the GUI in the main thread.

I think I'm doing something stupid here. Any clues?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Updating the progress bar should happen in the Swing Event Dispatch thread. Your heavy method should run in its own thread. It shouldn't block the progress bar. You can update the GUI from anywhere, using javax.swing.SwingUtilities#invokeLater(Runnable doRun). If you're using Java6.0, you can look into javax.swing.SwingWorker which can help you working with the Event Dispatch thread.

There's also a tutorial here.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles Mulloy wrote:I've thought about using threads, but I was taught to use them in separate classes. If I do this then, I can't let one thread "go to town" and simultaneously update the GUI in the main thread.

I don't see what "separate classes" have to do with the problem. There's nothing to stop an object in one class from doing something with an object in another class.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe is right; we even have an FAQ for this: JProgressBar Doesn't Update. Moving to Swing.
 
Charles Mulloy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That FAQ got it done. Thanks for the help!

I barely understand whats going on, but I'll defiantly have it figured out before too long.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!