Using Thread.sleep(..) and Swing Components

K

karpthomas

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
 
R

RedGrittyBrick

Peter said:
[...]
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
 
K

karpthomas

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
 
J

John B. Matthews

RedGrittyBrick said:
Peter said:
[...]
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>
 
D

Daniel Pitts

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.
 

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

No members online now.

Forum statistics

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

Latest Threads

Top