Click to See Complete Forum and Search --> : Sleep and refresh problem
-manu-
May 9th, 2004, 02:00 PM
Hi gurus
Here is my problem : I would like to display text in 3 JLabel, but I would like my 3 texts not to appear at the same time, but with one second between each of them.
My code is below, but it doesn't work : nothing happens during 3 secondes and then everything appear.
What should I do ?
I've tryed to display text in a separate thread but it doesn't work either.
public static void main(String[] argv)
{
// create panel with 3 labels
JPanel panel = new JPanel(new FlowLayout());
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();
panel.add(label1);
panel.add(label2);
panel.add(label3);
// display text
label1.setText("text1");
pause(1000);
label2.setText("text2");
pause(1000);
label3.setText("text3");
pause(1000);
// create and display frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.show();
}
public static void pause(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch(Exception e){}
}
Thank you for you help.
dlorde
May 9th, 2004, 03:14 PM
Firstly, nothing will be displayed until the frame.show() method is run, so it doesn't matter how long you wait between setting the label texts, they will all be in place before the frame that holds them is displayed.
Secondly, even if you set the label texts after the frame is displayed, e.g. by using a timer callback or responding to user input, etc. in a method, the texts will all appear together because the code that displays the changes can't run until the method completes - it's the same thread of execution. Note that the pause() method makes the current thread sleep - unfortunately this is the same thread that will display the changes.
The answer is to run a separate thread that will set the label texts with a delay between each. This will allow the original thread to update the display independently.
Put this code just before the show() call:Thread thread = new Thread() {
public void run() {
pause(1000);
label1.setText("text1");
pause(1000);
label2.setText("text2");
pause(1000);
label3.setText("text3");
}
};
thread.start();This creates and starts a separate thread that sets the label texts after pausing each time.
If you cannot describe what you are doing as a process, you don't know what you're doing...
W. E. Deming
-manu-
May 9th, 2004, 05:06 PM
Thanks for your help.
Actually my program is a bit more complex than the one I posted, and it's more something like that :
Thread thread1 = new Thread() {
public void run() {
pause(5000);
label1.setText("aaaaaaaa");
}
};
thread1.start();
Thread thread2 = new Thread() {
public void run() {
pause(5000);
label1.setText("bbbbbbbbb");
}
};
thread2.start();
I would like :
- pause 5 sec
- aaaaaaaa
- pause 5 sec
- bbbbbbbbb
I have :
- pause 5 sec
- bbbbbbbbb
Why ?
Thanks again
dlorde
May 9th, 2004, 07:15 PM
Why ?Because you've started 2 threads, both of which wait for 5 seconds before setting the label text. The second thread is run a few milliseconds after the first, so overwrites the label text a few milliseconds after the first thread has changed it. That's too fast for you to see the first change.
I don't know why you're using 2 threads, why not just use one thread and change the label1 text twice (or however many times you want) with a pause each time? (in other words, use the code I posted, but set the text of label1 each time instead of labels 2 and 3).
Why make things complicated? Simple is easier to understand - and it works...
cjard
May 10th, 2004, 04:49 AM
i think -manu- needs to read up a bit more about Threads and multithreaded programming, at the moment, his coding style seems to indicate that he believes threads to run consecutively, not concurrently
-manu- .. then you start a thread running, it runs at the same time as other threads. they dont run one after each other..
in school athletics, you probably ran 100m sprint, where all the racers set off at the same time, and you probably also ran a Relay race where you had to pass a baton to the next runner before he could go..
at the moment, youre thinking that multithreaded programming is like a Relay race.. it isnt.. its more like a proper 100m Sprint.. when you set another thread off running, off he goes, and runs at the same time as the other thread.
now do you see why starting 2 threads, who both sleep for 5 seconds then set the label text, is like creating 2 new humans, getting them to wait for 5 seconds, then set off running and try and decide who set off first..
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.