Reputation: 1
I am making a cosmetic SWING GUI program, in NetBeans, just for some fun with friends and it involves a loading screen that really only takes a few milliseconds but I want it to take a few seconds to finish, which is where I am trying to use the Thread.sleep() . The loading is a For-Loop and I know you have to throw InterruptedException but being that it is made with the NetBeans GUI builder you can't add it, I have tried just copy and pasted into a different project but it just comes up with errors. So my question is how I would get the Thread.sleep() to work or if there is some alternative way of causing the program to pause. Thank you
Edit: Basically... How do I get a loop to pause every interval so that five loop intervals take five seconds.
Upvotes: 0
Views: 1618
Reputation: 46841
Never use Thread.sleep()
in Swing GUI application. Alternatively use Swing Timers for the same behavior.
Please have a look about How to Use Swing Timers
Read more about Thread.Sleep alternative in Java
You can use Swing timers in two ways:
To perform a task once, after a delay.
For example, the tool tip manager uses Swing timers to determine when to show a tool tip and when to hide it.
To perform a task repeatedly.
For example, you might perform animation or update a component that displays progress toward a goal.
Upvotes: 2