Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Alternative to thread.sleep?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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.

    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.
    Last edited by jm24; February 12th, 2012 at 01:45 AM.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Alternative to thread.sleep?

    I don't think sleeping the EDT is a good idea.

    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.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    jm24 (February 12th, 2012)

Similar Threads

  1. Problem with thread.sleep()
    By stormforce in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 19th, 2011, 02:10 AM
  2. How to convert thread.sleep() into a timer in an Applet
    By all_pro in forum Java Theory & Questions
    Replies: 2
    Last Post: April 14th, 2011, 07:45 AM
  3. [SOLVED] Giving Thread.sleep( ); a decimal value
    By Knox in forum Threads
    Replies: 2
    Last Post: April 9th, 2011, 08:55 PM
  4. [SOLVED] Thread.sleep() in while loop not working
    By mds1256 in forum Threads
    Replies: 4
    Last Post: January 13th, 2011, 06:02 AM
  5. Thread Sleep, Timer, Button Question
    By tabutcher in forum Java Theory & Questions
    Replies: 1
    Last Post: May 1st, 2010, 02:54 AM