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.
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 () .
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.
main()
So, you can sleep peacefully in your main topic.
- . , Swing, , .
java, . , , , .
void Delay(Long ms){ Long dietime = System.currentTimeMillis()+ms; while(System.currentTimeMillis()<dietime){ //do nothing } }
: 5 Delay (5L)
. , , , , . , .
, , . , , , . , , , . , , , . ? - , , , . , java . , , , , , .
- , , , , , . , sleep(), , , , .
sleep()
, , , , Thread.sleep() , .
Thread.sleep()
Source: https://habr.com/ru/post/1741790/More articles:How should a web developer approach the creation of a browser toolbar? - cross-browserSQL Server 2008 Full Text Rebuild - Large memory issue - sql-server-2008Is the Hello World compiler for F #? - f #How to get around Python "Problems with WindowsError set incorrectly"? - pythonWhat is the easiest way to do background loading in Wicket? - javaIn OSX, how to determine the position of windows and which window is active? - objective-cHow to do simple Exec work in TFS Build 2010 - tfsEditable QTreeView with expandable cells - qtMemory exhaustion imagejpeg - phpEmacs highlighting in Asm mode - indentationAll Articles