2

I'm trying to restrict the user from interacting with the program for a certain period of time, but sadly, with no success. I've tried using Thread.sleep, but as soon as the sleep action ends, the users' input catches up and keeps executing until it's finished.

For example, if I use the code below, the program will act as mentioned above: it'll sleep for the provided period of time (1,5s) and then the users' actions will catch up (it'll print "After waiting" as much times as the user clicked while the Thread.sleep was active). So how would I prevent this? Any suggestions would be greatly appreciated.

public void mouseClicked(MouseEvent e) {
        print();
    }

public void print() {
    System.out.println("After waiting.");
    try {
        Thread.sleep(1500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
  • One way is to keep a list of components/elements you want to enable/disable. Then use a timer. Disable everything in the list, then fire an event that enables everything in the list. – matt May 06 '16 at 10:11
  • I think you better use a 'glasshour' modal dialog. Here is the docs: https://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html And this is a related SO QA: http://stackoverflow.com/questions/20269083/make-a-swing-thread-that-show-a-please-wait-jdialog – Tamas Rev May 06 '16 at 10:45
  • You're thinking at the wrong level. Instead of literally preventing the user from providing any input to the program, you should change the way the program _responds_ to the user's attempted interaction. That is, continue to accept input from the user, but then ignore the input, or better still: Provide some kind of feedback that means, "Yes, I can hear you, but I'm ignoring you because I'm busy right now." – Solomon Slow May 06 '16 at 13:49

1 Answers1

2

The reason is that Thread.sleep() will block the UI Thread. So by clicking the first time, you will get an output and your application will store the following events from clicking again during the time for sleeping. Then, after your GUI-Thread comes back from sleeping, the events are released and some of the print-prompts are executed until it sleeps. Because

It is not very trivial to block the UI-Thread. If you want to prevent user input, you should disable the button or the Node in general which fires the event and enable it if its processing was finished.

Supahupe
  • 515
  • 2
  • 11
  • While I think you are correct, I still don't know how prevent user input. I'm creating a replica of the game _"Boom Dots"_ (you're a ball that shoots other balls. If you miss - you die, if you hit it - you score a point and spawn another time to do the same). So what I want to do, is that the users' clicks wouldn't be registered and the ball wouldn't come back to his starting position (his Y coordinate wouldn't change). How would I go about doing so? Click [here](http://textuploader.com/5bszv) (starting class) and [here](http://textuploader.com/5bsz2) (dot class) for the code. –  May 06 '16 at 09:59
  • 1
    I think in this case I would try to solve this with a boolean flag in your dot. If you fired the dot first set it to true or false and prevent further actions by this. To allow further user actions, identify the point to set back the flag to true/false. – Supahupe May 06 '16 at 10:12
  • I was thinking of doing so. I'll try that. Thanks for the help. –  May 06 '16 at 13:15