Is that to implement JProgessBar, we need do in different thread?

J

James

without using antoher thread, there is no way to do it right?
when something is processing, it will not response to progressbar?
anyone can give some example(code) on it?
eg. like processing for loop 10000 time
the progress bar showing the percentage.
 
J

joeking

James said:
without using antoher thread, there is no way to do it right?
when something is processing, it will not response to progressbar?
anyone can give some example(code) on it?
eg. like processing for loop 10000 time
the progress bar showing the percentage.

The progress bar should update, unless you are doing your
processing inside a UI event handling method, in which case
you'll be running under Swing's own thread, which would likely prevent
other components from updating until your method exits
and returns control back to Swing.


-FISH- ><>
 
D

Daniel Grieves

Not sure I understand your question but here is what I do when using the
JProgressBar:

1) As a previous post explained, if you call setValue() from the
event-handling thread, the component will not update until you release the
event-handling thread. This means that you cannot be performing your
time-consuming task on the event-handling thread (usually a bad idea anyway)
so start another thread and do the heavy lifting there.

2) If you call setValue() from any thread _other_ than the event-handling
thread, you are (to the best of my knowledge) breaking the contract that a
method call to a visible Swing component must be made from the
event-handling thread. So as you're cruising along in a different thread,
performing a time-consuming task, any calls you make to setValue() must be
put onto the event-handling thread. The easiest way to do this is to use
the SwingUtilities.invokeLater() method.

3) If you use this method often enough, eventually you will decide to
sub-class JProgressBar and provide your own thread-safe alternative to the
setValue() method.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top