aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Wait in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Wait in Java" Watch "Wait in Java" New topic
Author

Wait in Java

Luke Alan
Greenhorn

Joined: Jun 09, 2009
Posts: 3
I've been trying to make my Java program wait and it's acting a bit weird.

I want it to show a String, then wait a number of seconds and then show another String.

I've used Thread.sleep() but that makes it wait and then display both Strings.

Is there anyway for me to get it to work like how I want it to?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18745
    
  40

I've used Thread.sleep() but that makes it wait and then display both Strings.


Assuming that you are writing to the System.out printstream.... It could be the streams buffering the data -- try calling the flush() method to flush the first string out before calling sleep().

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Luke Alan
Greenhorn

Joined: Jun 09, 2009
Posts: 3
Henry Wong wrote:
I've used Thread.sleep() but that makes it wait and then display both Strings.


Assuming that you are writing to the System.out printstream.... It could be the streams buffering the data -- try calling the flush() method to flush the first string out before calling sleep().

Henry


I'm sending it to a JTextArea and I'm also reading the String from a file. Should have mentioned that to begin with, sorry. I tried flushing the BufferedReader and that hasn't worked.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18745
    
  40

I'm sending it to a JTextArea and I'm also reading the String from a file. Should have mentioned that to begin with, sorry. I tried flushing the BufferedReader and that hasn't worked.


Are you doing the sleep() in the event handler? You can't do that. The GUI needs the event dispatching thread to do stuff, so it won't do anything until you are done.

The strings won't appear until your event handler is done. In order to have the effect of waiting a few seconds, you need to schedule for an event seconds later, return from the event handler, and have you new scheduled event handler print the other string.

See the javax.swing.Timer class for details on scheduling an event at a later time.

Henry
Luke Alan
Greenhorn

Joined: Jun 09, 2009
Posts: 3
Henry Wong wrote:
I'm sending it to a JTextArea and I'm also reading the String from a file. Should have mentioned that to begin with, sorry. I tried flushing the BufferedReader and that hasn't worked.


Are you doing the sleep() in the event handler? You can't do that. The GUI needs the event dispatching thread to do stuff, so it won't do anything until you are done.

The strings won't appear until your event handler is done. In order to have the effect of waiting a few seconds, you need to schedule for an event seconds later, return from the event handler, and have you new scheduled event handler print the other string.

See the javax.swing.Timer class for details on scheduling an event at a later time.

Henry


Well the method itself is activated using an event handler, but it doesn't run in the event handler.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18745
    
  40

Luke Alan wrote:
Well the method itself is activated using an event handler, but it doesn't run in the event handler.


By event handler, I mean the event handling thread, this means it can't be called by the event handler either. Or are you saying that you started a separate thread to call this code?


and BTW, I doubt that you did, because only the event dispatching thread is allowed to change running swing components.

Henry
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What do you mean by "activated using an event handler, but it doesn't run in the event handler"?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38393
    
  23
Too difficult a question for us beginners. Moving.
Matthew Cox
Greenhorn

Joined: May 25, 2009
Posts: 29
When you make any calls to other methods within an Event Dispatch Thread (EDT)... effectively those calls and their code will be executed by the EDT.

Bottom line : You need to either have the results returned or stored in another DS that can be accessed then printed after the EDT finishes handling the event OR
You have to invoke the method on a seperate thread. I think you will want to do the former option.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Read the tutorial: Swing Concurrency Tutorial, it explains those concepts and tells you how to write proper code using them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wait in Java