How and why would you use the java.awt.EventQueue?

Sandip Chitale

Well there are many reasons.

  1. Say, you wanted to catch all the mouse events across all the GUI of your application. Without tapping the EventQueue you will have to add a mouse listener and mouse motion listener to every component. You will have to also add container listener to every container so that you can then add mouse listener to newly added components. You will still not know the existence of new frames if it was not explicitly annouced in the program.

  2. If you wanted to detect inactivity on the EventQueue so that you could perform low priority background task (like the animation on the help guy in MS word when user seems to be inactive possibly because the user is wondering what to do next etc). You could start a time after every event. If next event comes before the timer expires, restart the timer. If the times expires you know that the event queue has been inactive that long and you could start the background task.

  3. You may want to automate the wait cursor as described in a recent article in JavaWorld. Here is the reference - http://www.javaworld.com/javaworld/javatips/jw-javatip87.html

  4. The JFC/Swing Accesibility utilities use the event queue to keep track of the GUI state.

  5. JavaStar (now discontinued) GUI testing tool used the event queue to keep track of the state of GUI and to do record and playback functionality for regression testing.

In fact the AWT engineers realized the importance of such a functionality and in JDK1.2 it is directly supported through

java.awt.Toolkit.getDefaultToolkit().addAWTListener(....)

The event queue class also supports push(EventQueue eq) and pop() APIs so that the users can install their own EventQueue.

In JDK1.1 the only way to hook the EventQueue was to modify the JDK-DIR/lib/awt.properties file and add an entry -

AWT.EventQueueClass=<your-eventqueue-subclass> which is dynamically loaded. This affects all the applications run from that installation though. You could turn on the special functionality using a system property or something though.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact