This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to make a time delay in Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to make a time delay in Java?" Watch "How to make a time delay in Java?" New topic

How to make a time delay in Java?

Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Condsider the situation.
1)I have a JFrame and this JFrame has a JButtton.
2)When ever I press this button it prints the "Hello There" by using the following -System.out.println("Hello There");
3)However when i press the button I do not want it to print it immediately. Lets say I wanted the program to wait 2 seconds then execute it.
How do I do this? Anyone have any ideas. Thanks in advance.All I could think of a for loop and let it count to a large number before executing the function. But that technique is not very accurate nore is it efficient.
Thanks for reading

yours respectfully<br />Avin Sinanan
Laudney Ren
Ranch Hand

Joined: Jan 06, 2002
Posts: 111
In the actionPerformed method of the actionListener, add:
before the System.println("...");
This is a simple but usual way of delaying.

" Veni, vidi, vici "<br />" I came, I saw, I conquered "
Jeremy Carter

Joined: Dec 10, 2011
Posts: 4

I'm guessing this has changed since 2002.

Just posting in case someone finds this post through a search.

sleep(10); doesn't do anything for me.
Thread.sleep(2000); would sleep for 2 seconds in Java 1.6.


It is sorta covered in the JavaRanch Style Guide.
subject: How to make a time delay in Java?
It's not a secret anymore!