File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Clock and text display question" Watch "Clock and text display question" New topic
Author

Clock and text display question

David Nelson
Greenhorn

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?

Cheers.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

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

Henry


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

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.

Cheers.
Stefan Krompass
Ranch Hand

Joined: Apr 29, 2004
Posts: 75
Hi,

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:


Stefan
David Nelson
Greenhorn

Joined: Nov 13, 2003
Posts: 22
Just got the point, thanks a lot
 
 
subject: Clock and text display question