• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Help  RSS feed

 
Moni Patil
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Want to Construct a javax.swing.Timer object such that the message �Happy Birthday to you� is displayed on a Text Field once per second
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key point here is something you have missed: what happens in between each second? Because it is no good displaying the message every second if the previously displayed one is still there!

That said, you should create your Timer instance with the appropriate delay (1000 milliseconds). In your class that creates the text field, implement ActionListener. In the actionPerformed method, change the text of the text field as required.

If you want to flash the text (i.e. on for one second, off for one second), generate a timer that pauses for 1000 milliseconds. Then implement some kind of boolean flag to say whether this second you should shown the message or erase it.

The Timer API docs will be able to help you too.
 
Amol Fuke
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moni,
You need to write one thread for this.Inside Threads run() method you put the code which display 'Happy birthday'.

Like
if(txtfld.getText().equals(""))
{
txtfld.setText(" H");
}
else if(txtfld.getText().trim().equals("H"))
{
txtfld.setText(" Ha");
}

Dont forget to put
Thread.sleep(milliseconds);
at the end.

Hope this helps you.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amol Fuke:
Moni,
You need to write one thread for this.Inside Threads run() method you put the code which display 'Happy birthday'.

Like
if(txtfld.getText().equals(""))
{
txtfld.setText(" H");
}
else if(txtfld.getText().trim().equals("H"))
{
txtfld.setText(" Ha");
}

Dont forget to put
Thread.sleep(milliseconds);
at the end.

Hope this helps you.


If this is what the OP wants then a better way to display the string is


[ September 06, 2005: Message edited by: Joanne Neal ]
[ September 06, 2005: Message edited by: Joanne Neal ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!