Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Using Thread.sleep(..) and Swing Components (http://www.velocityreviews.com/forums/t637486-using-thread-sleep-and-swing-components.html)

karpthomas@googlemail.com 09-29-2008 05:30 PM

Using Thread.sleep(..) and Swing Components
 
Hi,

I want to update a component several times.
When the user clicks a button, another component
- e.g. a JProgressBar, but it seems that this doesn't matter -
should be updated several times.

The code looks like following:

private void goButtonActionPerformed(java.awt.event.ActionEvent evt) {
for (int i = 0; i <= 10; i++) {
try {
Thread.sleep(300);
progressBar.setValue(i);
} catch (InterruptedException e) {
// do something
}
}
}

The problem is, that the component seems to be repainted only, when
the method
is finished.

What am I doing wrong?

Regards, Thomas

RedGrittyBrick 09-29-2008 06:37 PM

Re: Using Thread.sleep(..) and Swing Components
 

Peter Duniho wrote:
> On Mon, 29 Sep 2008 10:30:37 -0700, <karpthomas@googlemail.com> wrote:
>
>> [...]
>> The problem is, that the component seems to be repainted only, when
>> the method
>> is finished.
>>
>> What am I doing wrong?

>
> You are delaying the event-dispatch thread by calling sleep().
>
> Operations that take any significant amount of time should be handled on
> a different thread, using the EventQueue.invokeLater() and
> EventQueue.invokeAndWait() methods to execute any code that is required
> to be executed on the EDT (such as setting the value of a progress bar
> control).
>


I find SwingWorker useful too. It simplifies the work of creating and
using threads when you also want to update the GUI.


Just my ยค0.02 worth

--
RGB

karpthomas@googlemail.com 09-29-2008 08:00 PM

Re: Using Thread.sleep(..) and Swing Components
 
Hi,

thank you. I think I'll have to learn about threads a
little bit more...

But using SwingWorker should be OK.

Thank you, Regards, Thomas

John B. Matthews 09-29-2008 08:32 PM

Re: Using Thread.sleep(..) and Swing Components
 
In article <gbr79n$o17$1@registered.motzarella.org>,
RedGrittyBrick <RedGrittyBrick@spamweary.invalid> wrote:

> Peter Duniho wrote:
> > On Mon, 29 Sep 2008 10:30:37 -0700, <karpthomas@googlemail.com> wrote:
> >
> >> [...]
> >> The problem is, that the component seems to be repainted only,
> >> when the method is finished.
> >>
> >> What am I doing wrong?

> >
> > You are delaying the event-dispatch thread by calling sleep().
> >
> > Operations that take any significant amount of time should be
> > handled on a different thread, using the EventQueue.invokeLater()
> > and EventQueue.invokeAndWait() methods to execute any code that is
> > required to be executed on the EDT (such as setting the value of a
> > progress bar control).

>
> I find SwingWorker useful too. It simplifies the work of creating and
> using threads when you also want to update the GUI.


Peter and RGB are correct. In addition, a SwingWorker back-port is
available, and it includes a nice example using JProgressBar:

<https://swingworker.dev.java.net/>

Alternatively, if your model is trivial and your drawing is simple, a
javax.swing.Timer may suffice:

<http://groups.google.com/group/comp.lang.java.help/msg/8d88d9d0bc7d947a>

Compare this with Knute Johnson's approach based on Runnable:

<http://groups.google.com/group/comp.lang.java.help/msg/fbe351cb311c9637>

--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews

Daniel Pitts 10-04-2008 02:14 AM

Re: Using Thread.sleep(..) and Swing Components
 
karpthomas@googlemail.com wrote:
> Hi,
>
> I want to update a component several times.
> When the user clicks a button, another component
> - e.g. a JProgressBar, but it seems that this doesn't matter -
> should be updated several times.
>
> The code looks like following:
>
> private void goButtonActionPerformed(java.awt.event.ActionEvent evt) {
> for (int i = 0; i <= 10; i++) {
> try {
> Thread.sleep(300);
> progressBar.setValue(i);
> } catch (InterruptedException e) {
> // do something
> }
> }
> }
>
> The problem is, that the component seems to be repainted only, when
> the method
> is finished.
>
> What am I doing wrong?
>
> Regards, Thomas

All GUI work happens on the same Thread, in a queue. Including
re-painting of components. Nothing can be repainted until your method
completes.

Instead of using Thread.sleep, you should consider using a
javax.swing.Timer or a SwingWorker.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


All times are GMT. The time now is 12:25 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.