0

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.

Rex8112
  • 1
  • 2
  • That's the problem with GUI builders. If you use them, you have to play with their rules. – Kayaman May 13 '14 at 18:32
  • Welcome to Stack Overflow! Your question is really difficult to understand; I'm not sure what problem you're trying to explain. You'll get better answers if you include the code you have now and are concise and precise about what problems you're having. – Scott Barta May 13 '14 at 18:50

1 Answers1

2

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.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76