I need to disable a JButton on on click and enable it again 2 seconds later, so for I've tried sleeping the ui thread from the event handler, but that leaves the button in a selected state where you can't read the text of the disabled button.
The code looks something like this:
JButton button = new JButton("Press me");
button.addActionListener(new ActionListener{
public void actionPerformed(ActionEvent ae) {
JButton button = ((JButton)e.getSource());
button.setEnabled(false);
button.setText("Wait a second")
button.repaint();
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
}
button.setEnabled(true);
button.setText("");
}
What happens is the button remains in a "being selected" state with no text for the 2 seconds and instantly disables and re-enables the button at the end, which isn't what I want, what I'm aiming for is the button to stay in a disabled state with text on it for two seconds and then re-enable.
What do I do?