Could someone explain what purpose EventQueue object serves?

83 Views Asked by At

Could someone explain what purpose EventQueue object serves in Java Swing?

I'm mostly confused about how queuing events is applied. Maybe providing common use of an event queue would be helpful.

1

There are 1 best solutions below

0
On

Because Swing is single threaded, it uses the EventQueue to hold instances of Runnable so that they can be dispatched sequentially and in the order posted, whether generated by the system or your program. The sine qua non usage is posting an event via EventQueue.invokeLater(), which ensures that Swing GUI objects are constructed and manipulated only on the event dispatch thread. You can replace the AWT EventQueue with your own implementation, as shown here, to see the events as they are placed in the queue.