Java EventQueue. When should I consider using it?
Asked Answered
C

3

6

I'm currently looking at the EventQueue class on the Oracle website: http://download.oracle.com/javase/1.4.2/docs/api/java/awt/EventQueue.html But I'm not sure when I should use it? Should I use it if my class has listeners for two or more Events?

Calves answered 24/2, 2011 at 0:48 Comment(1)
You may want to use the latest edition (and the documentation for this).Frager
M
6

Normally you don't have to submit any events to the EventQueue, this all happens "automatically" when the user does his actions (like mouse clicks and such), or when the system thinks your window needs to be repainted.

The only two methods I'm using regularly are EventQueue.invokeLater and EventQueue.invokeAndWait() (less often). Use one of them if you are doing some action outside of the EDT (event dispatch thread) and then want to do some changes to the GUI (like adding or removing a component to/from a container), as such actions should occur only on the EDT.

Meredi answered 24/2, 2011 at 1:23 Comment(3)
Why are events placed in the EventQueue? Why can't they be directly dispatched to the component as and when they have occurred?Pomology
The idea is that there is only one thread which handles all this GUI stuff, not several ones (which simplifies much things). So if something happens (like an user interaction) while this thread is occupied, the event is first put into the queue, and then dispatched when the EDT has time.Frager
Are there any other events that are not placed in the queue but dispatched directly to the component?Pomology
C
6

I've never used it in 14 years of Java programming.

Cohligan answered 24/2, 2011 at 1:23 Comment(0)
A
1

AWT uses these to take care of things for your GUI under the hood. Normally you wouldn't use these it unless you are building some GUI engine on top of AWT. Like Paulo said there is this important method invokeLater but usually you can achieve the same effect using SwingUtilities.invokeLater this way seems to be used lot more often.

Ardy answered 24/2, 2011 at 1:30 Comment(1)
SwingUtilities.invokeLater and EventQueue.invokeLater do exactly the same, but the first one is some characters longer, and so always has the effect that my line (including the new Runnable(){public void run() {) does not fit on my 80-char width screen, while it fits with EventQueue.invokeLater. So, clear decision :-)Frager

© 2022 - 2024 — McMap. All rights reserved.