Java EventQueue. When should I consider using it?

3.1k views Asked by At

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?

3

There are 3 answers

3
Paŭlo Ebermann On BEST ANSWER

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.

0
user207421 On

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

1
m0s On

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.