• Post Reply Bookmark Topic Watch Topic
  • New Topic

Timers  RSS feed

 
hatim osman
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you must set the Opaque property of the JLabel to true "label.setOpaque(true)" this will make the component responsible for painting it's background "Swing components are transpernt".
use the following
//assuming the actionPerformed method is encapsulated in the same class
Timer timer = new Timer(5000, this);
...
...
...
}
public void actionPerformed(ActionEvent e) {
label.setBackground(Color.green);
}

This will work if and only if the label object is accessable to the actionPerformed method. In your case, your JLabel object is defined as a local variable within the constructor. Make it an instance variable.
In addition, the timer above will start after 5 seconds and repeats every 5 second. to make a one shot timer add the following:

timer.setInitialDelay(0); //no delay
timer.setDelay(5000);
timer.setRepeats(false);

I hope this can help

Hatim
SCJP
SCJD preparing
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!