It is possible to trigger on events occuring with the JVM using the AWTEventListener class. There won't be fine grain control nor information about what event occurred, but that can be derived from the expected events.
This is an example of how to use the AWTEventListener to monitor for window events such as opening windows and closing windows, as well as windows gaining and losing focus.
The first step is to define an AWT event listener. This listener makes the newly created frame invisible. It's almost immediate, but the frame will appear briefly before disappearing.
AWTEventListener listener = new AWTEventListener() {
public void eventDispatched(java.awt.AWTEvent event) {
if (event.getID() == WindowEvent.WINDOW_OPENED) {
Frame[] frames = Frame.getFrames();
int frm_idx = frames.length-1;
System.out.println( " making frame " + frm_idx
+ " (" + frames[frm_idx].getTitle()
+ ") invisible" );
frames[frm_idx].setVisible(false);
}
}
};
Once the listener object has been created, it needs to be registered to become active using addAWTEventListener():
Toolkit.getDefaultToolkit().addAWTEventListener(listener, AWTEvent.WINDOW_EVENT_MASK);
To disable the listener, use removeAWTEventListener():
Toolkit.getDefaultToolkit().removeAWTEventListener(listener);
No comments:
Post a Comment