Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.awt.EventQueue.invokeLater explained

I am very curious why do we have to use java.awt.EventQueue.invokeLater to control swing components.

Why can't we do that in normal thread? What exactly is going on behind the scenes? From what I have noticed if I have a JFrame I can set visibility to true or false from the main thread without getting any errors, and it does seem to work. So what exactly do I achieve by using java.awt.EventQueue.invokeLater? I am also fully aware that I can use SwingUtilities.invokeLater but as explained here, they seem to be one and the same thing.

Thanks to anyone for their explanation. Hopefully this is a valid question.

EDIT: to answer wumpz question We can create a jframe

JFrame frame = new JFrame("Hello world"); frame.setSize(new Dimension(300, 300)); frame.setPreferredSize(new Dimension(300, 300)); frame.setMaximumSize(new Dimension(300, 300)); frame.setMinimumSize(new Dimension(300, 300)); frame.setVisible(true); frame.pack(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

And on the same thread it was created do the following.

for (int i = 0; i < 34; i++) {     System.out.println("Main thread setting to "+(!frame.isVisible()));     frame.setVisible(!frame.isVisible()); } 

And no complaints.

like image 782
Quillion Avatar asked Mar 20 '14 13:03

Quillion


People also ask

What is EventQueue invokeLater in Java?

invokeLater. public static void invokeLater(Runnable runnable) Causes runnable to have its run method called in the dispatch thread of the system EventQueue . This will happen after all pending events are processed.

What is invokeLater in Java?

An invokeLater() method is a static method of the SwingUtilities class and it can be used to perform a task asynchronously in the AWT Event dispatcher thread. The SwingUtilities. invokeLater() method works like SwingUtilities. invokeAndWait() except that it puts the request on the event queue and returns immediately.

What is SwingUtilities invokeLater new runnable ()?

SwingUtilities class has two useful function to help with GUI rendering task: 1) invokeLater(Runnable):Causes doRun. run() to be executed asynchronously on the AWT event dispatching thread(EDT). This will happen after all pending AWT events have been processed, as is described above.


2 Answers

The complete Swing processing is done in a thread called EDT (Event Dispatching Thread). Therefore you would block the GUI if you would compute some long lasting calculations within this thread.

The way to go here is to process your calculation within a different thread, so your GUI stays responsive. At the end you want to update your GUI, which have to be done within the EDT. Now EventQueue.invokeLater comes into play. It posts an event (your Runnable) at the end of Swings event list and is processed after all previous GUI events are processed.

Also the usage of EventQueue.invokeAndWait is possible here. The difference is, that your calculation thread blocks until your GUI is updated. So it is obvious that this must not be used from the EDT.

Be careful not to update your Swing GUI from a different thread. In most cases this produces some strange updating/refreshing issues.

Still there is Java code out there that starts a JFrame simple from the main thread. This could cause issues, but is not prevented from Swing. Most modern IDEs now create something like this to start the GUI:

public static void main(String args[]) {     java.awt.EventQueue.invokeLater(new Runnable() {         public void run() {             new NewJFrame().setVisible(true);         }     }); } 
like image 194
wumpz Avatar answered Oct 01 '22 14:10

wumpz


All supported platforms offer single-threaded graphics libraries. Swing is cross-platform. Therefore, Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

As an aside, SwingUtilities.invokeLater() is a cover for EventQueue.invokeLater() since version 1.3.

like image 40
trashgod Avatar answered Oct 01 '22 14:10

trashgod