0

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
wayne
  • 1
  • 2
  • *"..EventQueue object serves in Java Swing?"* Although Swing uses it, the `EventQueue` is from AWT. – Andrew Thompson Apr 28 '16 at 03:05
  • I most cases, you don't interact with the `EventQueue`. The `EventQueue` is a queue of events, which is processed by the Event Dispatching Thread, these events are delivered to components and interested parties in order to provide notification of something happening, like a mouse click or key board event. The `EventQueue` provides a concept of "order" where events can only occur in a linear fashion, one after the other and generally within a time sequenced order (so my key stroke can't bet my mouse event if I moved the mouse first) – MadProgrammer Apr 28 '16 at 07:33

1 Answers1

2

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045