Java EventQueue. When should I consider using it?
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.
I've never used it in 14 years of Java programming.
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.
精彩评论