File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Clock and text display question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Clock and text display question" Watch "Clock and text display question" New topic

Clock and text display question

David Nelson

Joined: Nov 13, 2003
Posts: 22
well, in my program, i want to add a clock to show player left time, and there is a button and a label, after user click the btn, label will show a sentence, after a while, e.g. 1 second, the sentence will be removed and the btn is actived again, so that user can click on it and show next sencence, and things go on.

currently, i use two threads, one is timer, another is textThread, in my run() method, if Thread.currentThread()==timer, i decrease remainTime by 1 and show it, if Thread.currentThread()==textThread, i display the text on the label and then sleep 1 second, but it has no reaction at all. am i on the right way? is there any alternative method?

Henry Wong

Joined: Sep 28, 2004
Posts: 20521

What GUI package are you using? AWT? SWT? Swing?


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
David Nelson

Joined: Nov 13, 2003
Posts: 22
AWT, anything different in my program? what should i suppose to do if i use Swing.

for the clock thing, i guess the choice is only Thread or Timer, so how about text showing, how can i remove them out after 1 every second.

Stefan Krompass
Ranch Hand

Joined: Apr 29, 2004
Posts: 75

perhaps you could solve your problem like this: when the button is pressed, display your sentence, deactivate the button and start a java.util.Timer. The run()-method of your timer removes the sentence from the label and reactivates the button. Some code snippets:

David Nelson

Joined: Nov 13, 2003
Posts: 22
Just got the point, thanks a lot
I agree. Here's the link:
subject: Clock and text display question
It's not a secret anymore!