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?
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.
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.
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.