Article View: borland.public.jbuilder.java.swing
Article #11672Re: JProgressBar problems
From: Tad Frysinger
Date: Sat, 17 Apr 1999 00:00
Date: Sat, 17 Apr 1999 00:00
67 lines
2954 bytes
2954 bytes
In article <370A0358.433014F1@teleatlas.com>, jan.lacluyse@teleatlas.com
says...
> i have some problems with the JProcessBar class !!
>
> When i want to change the value of a JProgressBar (setValue(int)) in a
> MouseListener method from a Button, it doesn't change immediately.
> It only change when all the functions are done in the MouseListener
> method, even i call the method repaint() after setting a new value
> and sets the minimum- and maximum value correctly.
>
> Example :
>
> void startButton_mouseClicked(MouseEvent e) {
> startButton.disable(); // property immediately
> changed
> aProgressBar.setMinimum(0);
> aProgressBar.setMaximum(100);
> for (int i=0 ; i<100 ; i++) {
> aProgressBar.setValue(i); // property not changed
> aProgressBar.repaint(); // property not changed
> }
> startButton.enabled(); // property immediately changed
>
> }
> // after this method the value of the ProgressBar will be 100
> (getValue()) and only will be repaint after this method !!!
>
>
> Can someone give me a solution !!!
>
> greetings Jan
>
> please reply at :
> jan.lacluyse@teleatlas.com
>
>
Painting is done on the same event thread as your application. You can
use paintImmediately() to "move it up in the queue". However, it is very
bad form for an event handler to take very long to do its thing.
Instead, you should spawn a thread to do the work you need to do,
updating the progress bar as the work goes along.
This, however, brings up another issue: Swing is not threadsafe. Thus,
if you attempt to update the progress bar from a separate thread, you
will potentially have paint deadlocks. To solve this, what you need to
do is to send your paint request from your separate thread (which you
created to keep your event handler from taking to long) and place it in
the main event thread queue (to solve the potential paint deadlock
problem). You do that by having your separate thread do its visual
updates using a SwingUtilities method. Here is what I use to update my
progress bars from separate threads:
void safeUpdateProgress( final int i )
{
// Update progress bar,
Runnable setProgressBar = new Runnable()
{
public void run()
{
progress.setValue(i);
}
};
SwingUtilities.invokeLater(setProgressBar);
}
Tad Frysinger (TeamB)
Technical Details
Message-ID:
<MPG.118254c3cb096fc49899bb@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<370A0358.433014F1@teleatlas.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,80ff9302a50d2f4b X-Google-Attributes: gid111d45,public From: tfrysinger@earthlink.net (Tad Frysinger) Subject: Re: JProgressBar problems Date: 1999/04/17 Message-ID: <MPG.118254c3cb096fc49899bb@forums.borland.com> X-Deja-AN: 467647846 References: <370A0358.433014F1@teleatlas.com> Organization: Freezone Enterprises Newsgroups: borland.public.jbuilder.java.swing