-2

I'm trying to make the program wait for the user to click on the GUI. But Thread.sleep() just sleeps the entire GUI. So all I can see is an empty window.

code:

while (!enteredField) {
    try {
        Thread.sleep(10);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Don
  • 3,876
  • 10
  • 47
  • 76
moe
  • 17
  • 4
  • you might be better off to use [Swing Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). – Ousmane D. May 10 '17 at 00:14
  • you can't use thread.sleep in a gui, you need to use another method – Ryan Turnbull May 10 '17 at 00:15
  • continue your logic from the click event handler – Scary Wombat May 10 '17 at 00:15
  • 1
    Possible duplicate of [how could i add a simple delay in a java swing application](http://stackoverflow.com/questions/12767367/how-could-i-add-a-simple-delay-in-a-java-swing-application) – Ryan Turnbull May 10 '17 at 00:16
  • First, start wih [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) – MadProgrammer May 10 '17 at 00:18
  • Consider having a look at [How to Use the Focus Subsystem](https://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html) and looking at the "Validating Input" section for some possible ideas – MadProgrammer May 10 '17 at 00:20
  • How do I use Swing Timers with this code? – moe May 10 '17 at 00:20
  • And naw, it's not really a duplicate – moe May 10 '17 at 00:21
  • Or, you're trying to wait for input to come from another window, have a look at [How to Make Dialogs](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) – MadProgrammer May 10 '17 at 00:21
  • And if I remove the while loop. The program just continues without my input. Which I don't want – moe May 10 '17 at 00:21
  • *"I'm trying to make the program wait for the user to click on the GU"* - You could use a [`WindowListener`](https://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html) or [`FocusListener`](https://docs.oracle.com/javase/tutorial/uiswing/events/focuslistener.html) - GUIs are event driven – MadProgrammer May 10 '17 at 00:23
  • @moe You've not provided enough context to your question for use to provide any type of accurate answer. The basic answer is, don't do this, what you could otherwise would come down to what you are trying to do in the long wrong. Remember, GUIs are event driven, they are not like console programs which run in a linear fashion – MadProgrammer May 10 '17 at 00:27
  • @Moe Consider providing a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) it will reduce the guess work and provide you with (hopefully) better answers – MadProgrammer May 10 '17 at 00:28
  • 1
    A UI application is always UI driven. It **always** just sits there and waits for events to happen. So alone your question title doesn't make sense. Probably you have some kind of "activity" going on; and you want to slow down that; or suspend that; and then you really really start with that link given to you by @MadProgrammer. Or that second link he gave to you. And "naw it is not a duplicate" doesn't help here - **you** should explain what you want to achieve; and make it clear why the other question doesnt help you. You got several GREAT advise by now. Step back and digest that! – GhostCat May 10 '17 at 06:51

2 Answers2

0

Swing events need to happen on the Event Dispatch Thread.

Have you tried

SwingUtilities.invokeAndWait(() -> Thread.sleep(20))

https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingUtilities.html#invokeAndWait-- ?

MaxPower
  • 881
  • 1
  • 8
  • 25
  • 1
    Doing this one the EDT will cause an exception to be raised. If you tried to do this from a background thread, you'd still be calling `Thread.sleep` on the EDT, which would not resolve the issue – MadProgrammer May 10 '17 at 00:25
  • 1
    I don't even know what that means. – moe May 10 '17 at 00:26
  • And I tried that code. It worked it showed everything. But it's not updating whenever I click on it. – moe May 10 '17 at 00:27
  • Do not call `Thread.sleep()` (or, for that matter, any method which blocks) in the EDT! It will cause the entire GUI to become unresponsive, which is probably not what the OP wants. As others have suggested, use a `Timer`. – ostrichofevil May 10 '17 at 01:05
  • @ostrichofevil Based on the information the OP has provided (or lack of) a `Timer` may not be suitable solution, a `ActionListener` might be a better solution - agree with everything else though – MadProgrammer May 10 '17 at 01:06
  • @MadProgrammer That's valid—just seemed like the most likely solution. – ostrichofevil May 10 '17 at 01:06
  • "I don't even know what that means." That's when you reach for the documentation, to some of which people have already provided you links, and Read the Fine Manual. You can also plug the unfamiliar terms into a search engine, along with the word "Java" to focus the results. You aren't going to learn Swing programming from SO. I'm afraid you're just going to have to study the topic. – Lew Bloch May 10 '17 at 07:38
0

I have had problem like this before. so you are not alone. I made mistakes like this.

Your Problem

while (!enteredField) {
    try {
        Thread.sleep(10);
    } catch (InterruptedException e) {
        e.printStackTrace();
     }
}

Solution

Thread thread = new Thread(() -> {
    while (!enteredField) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
         }
    }
});
thread.start();

* Detailed Response*


' What you did was that you created a thread inside a while loop. Basically You paused the whole GUI for 10 milliseconds, the whole program for matter of fact. Furthermore, I do not understand what your objective is. You said that you want to wait for the user to click on the GUI.Suggestions would be, listen for the button click. for example Button.setOnAction(() -> {}); That would do he job instead of sleeping. I just the only case that you would want to pause or sleep is if you are animating somethings.

Abdul Sheikh
  • 547
  • 4
  • 6