Can anyone tell me what is SwingUtilities.invokeLater(Runnable) method in Java and how it perfoms?
Can anyone tell me what is SwingUtilities.invokeLater(Runnable) method in Java and how it perfoms?
The static utility method invokeLater(Runnable) is intended to execute a new runnable thread from a Swing application without disturbing the normal sequence of event dispatching from the Graphical User Interface (GUI). The method places the runnable object in the queue of Abstract Windowing Toolkit (AWT) events that are due to be processed and returns immediately. The runnable object’s run() method is only called when it reaches the front of the queue. The deferred effect of the invokeLater(Runnable) method ensures that any necessary updates to the user interface can occur immediately, and the runnable work will begin as soon as those high priority events are dealt with. The invoke later method might be used to start work in response to a button click that also requires a significant change to the user interface, perhaps to restrict other activities, while the runnable thread executes.
If possible also provide me the Example of SwingUtilities.invokeLater(Runnable) method.
Thanks
I will explain you the example, how to implement the SwingUtilities.invokeLater(Runnable) method in the program.
Code:SwingUtilities.invokeLater( new Runnable(){ public void run(){ outputArea.append(messageToDisplay); } } );
what does invokeLater actually do, along with the nested method within it.
The Swing Library is not thread safe, which means that only one thread may create or modify swing objects. This is the AWT Event thread. Whenever Swing calls a listener method, it is executed on the AWT Event thread, so you may modify Swing objects here.
InvokeLater causes Swing to run a given runnable when it can on the AWT Event thread. You should use invokeLater any time you want to modify a Swing object outside of a listener method.
Hope this makes you clear with Swing and AWT.
Bookmarks