Results 1 to 9 of 9
  1. #1
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Thread.sleep() alternative

    Hello!

    I'm looking for an alternative to thread.sleep(). I'm appending several lines of text to a JTextArea, but for presentation purposes I want each line to be printed half a second after the last. Thread.sleep() seems to be every google result I find, however this locks up my entire program for the duration of time and then prints everything all at once.

    Any hints?

    Thanks!

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: Thread.sleep() alternative

    You can use Swing Timer (javax.swing.Timer). It will allows you to do something periodically. Here is a link that will help you: How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features).

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Thread.sleep() alternative

    Quote Originally Posted by Razion View Post
    Hello!

    I'm looking for an alternative to thread.sleep(). I'm appending several lines of text to a JTextArea, but for presentation purposes I want each line to be printed half a second after the last. Thread.sleep() seems to be every google result I find, however this locks up my entire program for the duration of time and then prints everything all at once.

    Any hints?

    Thanks!
    It's locking up your system because you are calling sleep() on the EDT which is a Bad Thing.
    If you don't know what the EDT is then you should read:
    Swing concurrency.
    Please do not ask for code as refusal often offends.

  4. #4
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Thread.sleep() alternative

    Quote Originally Posted by wsaryada View Post
    You can use Swing Timer (javax.swing.Timer). It will allows you to do something periodically. Here is a link that will help you: How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features).
    Is there any way of doing this that doesn't involve the timer triggering an ActionListener? I need to append a series of Strings on program load, with a delay in between each string.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Thread.sleep() alternative

    Quote Originally Posted by Razion View Post
    Is there any way of doing this that doesn't involve the timer triggering an ActionListener? I need to append a series of Strings on program load, with a delay in between each string.
    So, what's wrong with using a Swing Timer? Nothing in your post above prevents you from using this useful tool.

  6. #6
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Thread.sleep() alternative

    Quote Originally Posted by Fubarable View Post
    So, what's wrong with using a Swing Timer? Nothing in your post above prevents you from using this useful tool.
    I must be confused. The example doesn't exactly fit my usage case.

    I create a timer, set the delay, and start it.
    Java Code:
    public void displayTitle(){
    		Timer timer = new Timer(300, new displayTitle(display));
    		timer.setInitialDelay(500);
    		timer.start();
    	}
    And to avoid conflicting with the ActionListener in my main class, I used a second class in the same file to hold a second ActionListener.

    Java Code:
    public void actionPerformed(ActionEvent event) {
    			TitleScreen ts = new TitleScreen();
    			switch(STATE){
    			case "LOGO1":
    				display.append(ts.displayLogo1());
    				STATE = "LOGO2";
    				break;
    			case "LOGO2":
    				display.append(ts.displayLogo2());
    				STATE = "LOGO3";
    				break;
    			case "LOGO3":
    				display.append(ts.displayLogo3());
    				STATE = "LOGO4";
    				break;
    			case "LOGO4":
    				display.append(ts.displayLogo4());
    				STATE = "LOGO5";
    				break;
    			case "LOGO5":
    				display.append(ts.displayLogo5());
    				STATE = "LOGO6";
    				break;
    			case "LOGO6":
    				display.append(ts.displayLogo6());
    				STATE = "LOGO7";
    				break;
    			case "LOGO7":
    				display.append(ts.displayLogo7());
    				STATE = "LOGO8";
    				break;
    			case "LOGO8":
    				display.append(ts.displayLogo8());	
    				STATE = "GETNAME";
    				createCharacter();
    				break;
    			}
    			
    		}
    I'm clearly doing something wrong. Any suggestions as to the proper way to go about this?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Thread.sleep() alternative

    It's tough for me to see what you may be doing wrong or right just yet. Consider telling us just what this code is supposed to be doing, what the rest of your program is supposed to be doing, what's working right, what's not working right, and anything else that we might need to know to understand your program, the problem you're trying to solve and the errors.

  8. #8
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Thread.sleep() alternative

    Quote Originally Posted by Fubarable View Post
    It's tough for me to see what you may be doing wrong or right just yet. Consider telling us just what this code is supposed to be doing, what the rest of your program is supposed to be doing, what's working right, what's not working right, and anything else that we might need to know to understand your program, the problem you're trying to solve and the errors.
    Luckily, I got it figured out using:

    Java Code:
    public void displayTitle(){
    		final TitleScreen ts = new TitleScreen();
    		ActionListener listener = new ActionListener(){
    			public void actionPerformed(ActionEvent event){
    				switch(STATE){
    				case "TITLE1":
    					display.append(ts.displayLogo1());
    					STATE = "TITLE2";
    					break;
    				case "TITLE2":
    					display.append(ts.displayLogo2());
    					STATE = "TITLE3";
    					break;
    				case "TITLE3":
    					display.append(ts.displayLogo3());
    					STATE = "TITLE4";
    					break;
    				case "TITLE4":
    					display.append(ts.displayLogo4());
    					STATE = "TITLE5";
    					break;
    				case "TITLE5":
    					display.append(ts.displayLogo5());
    					STATE = "TITLE6";
    					break;
    				case "TITLE6":
    					display.append(ts.displayLogo6());
    					STATE = "TITLE7";
    					break;
    				case "TITLE7":
    					display.append(ts.displayLogo7());
    					STATE = "TITLE8";
    					break;
    				case "TITLE8":
    					display.append(ts.displayLogo8());
    					STATE = "GETNAME";
    					createCharacter();
    					break;
    				}
    			}
    		};
    		Timer timer = new Timer(400, listener);
    		timer.start();
    	}
    Do I need to stop the timer to save resources or something? If so, when do I stop it? I would imagine calling stop() right after starting it would terminate it before it had a chance to execute more than once.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Thread.sleep() alternative

    Add a second listener that simply checks the current STATE and stops the timer.
    In order to do this you'll need to make the timer 'final' so it can be referred to in the second listener...which may as well be anonymous:
    Java Code:
    final Timer timer = new Timer(400, listener);
    timer.addActionListener(new ActionListener() {
       // fill in the details here.
    });
    timer.start();
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Thread.sleep
    By Gog in forum New To Java
    Replies: 3
    Last Post: 01-13-2011, 08:14 AM
  2. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  3. how to reduce the thread sleep time and wake up the thread
    By baktha.thalapathy in forum Threads and Synchronization
    Replies: 2
    Last Post: 06-24-2010, 07:36 PM
  4. Do I have to put Thread to sleep?
    By atch in forum New To Java
    Replies: 0
    Last Post: 03-08-2010, 10:21 AM
  5. thread.sleep
    By jc5 in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-02-2009, 11:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •