You need to set up a Swing Timer to handle the countdown on the button. See the following on timers: How to Use Timers .
The behavior you are getting right now is because you are sleeping in the thread that is creating the UI. Create the UI first and after it is set visible, start the Timer task that updates the label on the button (and it's behavior if needed).
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
You could use the following for the countdown
class ButtonCountdown implements ActionListener {
int count=0;
JButton button = null;
public ButtonCountdown(final JButton button, int count){
this.count = count;
this.button = button;
}
public void actionPerformed(ActionEvent e) {
if (count>0){
button.setText(String.valueOf(--count));
} else {
button.setText("Done!");
}
}
}
and then start it like so after you have the UI all set up
Timer koTimer = new Timer(1000, new ButtonCountdown(ko, 5));
koTimer.start();
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847