• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wait in Java

 
Luke Alan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21184
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Luke Alan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21184
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21184
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "activated using an event handler, but it doesn't run in the event handler"?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for us beginners. Moving.
 
Matthew Cox
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21111
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the tutorial: Swing Concurrency Tutorial, it explains those concepts and tells you how to write proper code using them.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic