Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

jprogressbar indeterminate mode

Thread view: 
tiewknvc9 - 22 Feb 2006 20:57 GMT
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...
James Westby - 22 Feb 2006 21:04 GMT
> Hi.
>
[quoted text clipped - 22 lines]
>
> 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
tiewknvc9 - 22 Feb 2006 22:27 GMT
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){
               
            }
        }
    }
James Westby - 22 Feb 2006 22:55 GMT
> I know that Im a newbie at this, but should I be creating a class that
> holds my JProgressBar and implements Runnable?
[quoted text clipped - 21 lines]
>         }
>     }

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
tiewknvc9 - 23 Feb 2006 16:09 GMT
very helpful.  I read it all.  Thanks!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.