Java GUI, you must pause the method without freezing the GUI as well

I know that this problem is caused by sleep or waiting for the main thread to be called, and the answer to how to solve this problem is to put this method in a separate thread and then make this thread sleep. But the code is a mess and really does not have time to figure it out and split it into separate threads, and it was interesting, is there any other way to do this? Even if this is not the purest or most common practice for working with graphical interfaces. I need only a second pause from the method.

+3
source share
5 answers

You cannot do this without creating a separate thread. Creating a thread in Java is simple. The only thing you need to pay attention to is that you can only touch the user interface through the main thread. For this reason, you need to use something like SwingUtilities.invokeLater () .

+8
source

It is not possible to sleep in the event stream and not cause the GUI to freeze. However, in Swing, an event stream is created and managed behind the scenes - the main stream (the one coming from the method main()) is not an event stream.

So, you can sleep peacefully in your main topic.

+1
source

- . , Swing, , .

0

java, . , , , .

     void Delay(Long ms){

       Long dietime = System.currentTimeMillis()+ms;
       while(System.currentTimeMillis()<dietime){
           //do nothing
       }
   }

: 5 Delay (5L)

0

. , , , , . , .

, , . , , , . , , , . , , , . ? - , , , . , java . , , , , , .

- , , , , , . , sleep(), , , , .

, , , , Thread.sleep() , .

-6

Source: https://habr.com/ru/post/1741790/


All Articles