• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Learning to use JProgressBar, having some problems. Need some help here.

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings everyone,
I'm thinking it would be nice to include a progress bar in an application I am developing to give the user an idea of what's going on behind the screen and how much's left to be done... I have never had to use the JProgressBar class, so I developed this application below to aid my understanding of the JProgressBar class without using the StringWorker class Sun's Java tutorial uses to explain how to use the JProgressBar class (truth is, I'm not sure I understood StringWorker totally) or some other Thread (how do I tie that Thread to this program? 'Haven't figured that out yet....).

The problem with this program is that when the start button is clicked to start the progress bar, the program blocks until the iteration in the program is completed and a full progress bar is displayed. What beats me is that I attempt to update the progress bar inside the actionPerformed method, which is inside the button listener class. But obviously, the JProgressBar (pb) is set when the only when the increasei method returns, that's why I'm getting this behaviour, right? How can I then get around this, without breaking the rule of allowing the realized component to be updated only inside the event dispaching thread?
Any useful suggestion would be greatly appreciated.
Thanks for reading up to this point.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another example form the Swing book by Robinson and Vorobiev is basic progress bar example
The action listener for the start button creates an instance of the Thread class called "runme". Its run method contructs Runneable objects within a loop. These objects update the progress bar. They are passed to SwingUtilities.invokeLater(). This method will schedule the execution of such runneable in the drawing-event handling thread by placing the runneable in the event handling queue. This is necessary to ensure all drawing and event handling occurs in one thread.
Please read Threads and Swing from the Java Tutorial.
 
Femi Alla
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jose,
It's quite clear to me now that it may not be possible to write a progress bar without an accompanying thread, somehow. Which was what I was trying to do....
Thanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic