Thread.sleep() blocking pack()

Hi i'm writing soem basic code for a downward counting clock, basically my problem is in pausing for a one second time interval. I've been using Thread.sleep(1000), and after each decrementing the timer towards zero.

What i want to do is after each decrement update a JLabel with the new value of the time, however when i run this with Thread sleep instead of pausing for a second then updating it just doesn't update the Jlabel, updated using setText().

I know it is working in principle as i also to check am printing the time out to console...

...any ideas why the pack/redrawing of the frame seems to be blocked and how i may be able to get around this. I have tried other pausing techniques such as taking system time and waiting till it reaches old system timeplus second...also doesn't work.

Thanks for any help in advance....the basic code of how mine is working is:

t = time as string

clock.setText(t);

System.out.println(t);

Thread.sleep(1000);

update time....etc

[1039 byte] By [Stavmana] at [2007-11-27 6:14:07]
# 1
Have a look at javax.swing.Timer
dwga at 2007-7-12 17:23:18 > top of Java-index,Desktop,Core GUI APIs...
# 2
> .any ideas why the pack/redrawing of the frame seems to be blocked Because your code is executing in the GUI Event Thread, so when you tell the Thread to sleep no painting can be done.
camickra at 2007-7-12 17:23:18 > top of Java-index,Desktop,Core GUI APIs...
# 3

> Because your code is executing in the GUI Event

> Thread, so when you tell the Thread to sleep no

> painting can be done.

Ok this makes sense, but surely once the thread has finished sleeping painting can be done? Or does the repeated calls to sleep ensure that no painting can ever be done whilst in this loop...also i implemented it using System.timemillisec and it gave the same effect, obviously tis would call the same Event so again why block the painting of the JFrame.

As for timers i'll look into them, i noticed them earlier but they didn't immediately strike me as the most direct solutions maybe i'll have to backtrack and figure them out as it seems i'm using current implementation ideas slightly incorrect.

Stavmana at 2007-7-12 17:23:18 > top of Java-index,Desktop,Core GUI APIs...
# 4

> Or does the repeated calls to sleep ensure that no painting can ever be done whilst in this loop

Exactly.

> As for timers i'll look into them

Its the simplest solution. The Timer fires every second and your code executes. You don't worry about any looping structure or anything. You just code the code you want to execute every second. When you want to stop the Timer you just use the stop() method.

Of course you can always create a separate Thread and add all your logic to that Thread. Then the Thread.sleep() method will cause that Thread to sleep, not the GUI event Thread, so the GUI is still free to respond to event and repaint itself.

camickra at 2007-7-12 17:23:18 > top of Java-index,Desktop,Core GUI APIs...