jprogressbar indeterminate mode

T

tiewknvc9

Hi.

I have a JProgressBar that has setIndeterminate(true). So that it will
show a small graphic moving back and forth while I do some lengthy
LENGTHY calculations.

The only problem is that the bar that is supposed to be bouncing
around... isn't.

It appears frozen.

I tried to validate and invalidate it. I also tried to paint it
immediately.

no change.

Im wondering if I need to set up another thread to handle the
JProgressBar and how I might be able to do that...

But I wonder if anyone knows of what I might be doing wrong? I dont
currently use threads in my application except for the default one.
How can I use a thread to repaint the progressbar?

Im lost.

Thanks for any helpful replies...
 
J

James Westby

tiewknvc9 said:
Hi.

I have a JProgressBar that has setIndeterminate(true). So that it will
show a small graphic moving back and forth while I do some lengthy
LENGTHY calculations.

The only problem is that the bar that is supposed to be bouncing
around... isn't.

It appears frozen.

I tried to validate and invalidate it. I also tried to paint it
immediately.

no change.

Im wondering if I need to set up another thread to handle the
JProgressBar and how I might be able to do that...

But I wonder if anyone knows of what I might be doing wrong? I dont
currently use threads in my application except for the default one.
How can I use a thread to repaint the progressbar?

Im lost.

Thanks for any helpful replies...
The JProgressBar is a swing component and as such needs to be accessed
only from the Exent Dispatching Thread (the one it is currently beiong
accessed from).

Your problem is that this thread is also the one doing your lengthy
calculation, so it is not free to do the updates. You need to free this
thread up. To do this create another thread *to do the lengthy
calculation*. The EDT is then free to keep updating the JProgressBar.
When the other thread completes it can send a message back to the EDT
that the JProgressBar should be stopped and let the rest of the program
continue. One way to do this is to use a method called invokeLater (I
forget where this method lives).

I'm sure there are plenty of articles on Google about how to use a
JProgressBar properly. If not then there will be at least articles on
threading in a Swing context.


James
 
T

tiewknvc9

I know that Im a newbie at this, but should I be creating a class that
holds my JProgressBar and implements Runnable?

and if I do, how can I code the run() so that the indeterminate is
updated properly?
So far I haven't been able to find enough information for my feeble
brain onine that explains what I should be doing to make it work...
just really stuck... I coded this run() in a class that extends
JProgressBar implements runnable.

public void run()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
Thread currentThread = Thread.currentThread();

while(runner == currentThread){
///what do I do here for
indeterminate? Its not even showing up now...
try{
Thread.sleep(10);
}catch (InterruptedException ie){

}
}
}
 
J

James Westby

tiewknvc9 said:
I know that Im a newbie at this, but should I be creating a class that
holds my JProgressBar and implements Runnable?

and if I do, how can I code the run() so that the indeterminate is
updated properly?
So far I haven't been able to find enough information for my feeble
brain onine that explains what I should be doing to make it work...
just really stuck... I coded this run() in a class that extends
JProgressBar implements runnable.

public void run()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
Thread currentThread = Thread.currentThread();

while(runner == currentThread){
///what do I do here for
indeterminate? Its not even showing up now...
try{
Thread.sleep(10);
}catch (InterruptedException ie){

}
}
}

No, leave the JProgressBar alone, it is fine how it is. If you move it
to a different thread it will not be on the EDT i mentioned in my last
post, and so will violate the rules of Swing.

You need to move your lengthy calculation to another thread, that is put
it within a runnable, it will then allow the JProgressBar to update.

i.e.


in the method that creates the JProgressBar and calls the method to
perform the really long conversation you may have something that looks
like this

JProgressBar progressBar = ...

....

someObject.performLengthyComputation();

....

Instead you want to create a new thread for the lengthy computation. It
might look something like this

JProgressBar progressBar = ...

.....

new Runnable() {

public void run() {

someObject.performLengthyComputation();

swingUtilities.invokeLater(new Runnable() {
public void run() {
//stop JProgressBar

//using the invokeLater() method ensures that the JProgressBar
//is accessed from the EDT only
});

}.run();

....


[Note not tested or compiled]

I saw something on another thread that would indicate that you can avoid
the second runnable to update the JProgressBar

JProgressBar progressBar = ...

.....

try {

new Runnable() {

public void run() {

someObject.performLengthyComputation();

//don't stop JProgressBar
}.run();

} finally {
//stop JProgressBar
}


[Again not tested or compiled]


There will be far more elegant ways of doing this, I'm just trying to
put the point across.

I would recommed that you look at the SwingWorker class. It is designed
to do exactly what you are trying to do.

http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html


James
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top