Alternative to thread.sleep?
Ok, so basically I want to use a swing timer and an actionListener and use thread.sleep in the middle of the actionPerformed method to make it wait 2 seconds before doing the next action. The problem is that when I add the code (and I use a try-catch to catch the interruptedException), it seems to ignore the wait and just go directly to the next action. Can anyone tell me the problem or suggest an alternative to using thread.sleep to wait a certain amount of time before going to the next action.
Here is the code for the actionPerformed method. The timer is firing every 3 seconds.
Code :
if(e.getSource() == t){
randButton = (int)(Math.random() * 16);
holes.get(randButton).setIcon(moleOut);
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
holes.get(randButton).setIcon(moleIn);
}
}
Btw it's supposed to change the icon of a random button (out of the 16 buttons in the arrayList holes), wait 2 seconds, and then change it back.
Re: Alternative to thread.sleep?
I don't think sleeping the EDT is a good idea.
Quote:
make it wait 2 seconds before doing the next action
Perform the first action then start a second timer that calls back after 2s at which time you can perform the second action.