File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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.


I agree. Here's the link:
subject: How to make a time delay in Java?
It's not a secret anymore!