Re: Listener for opening
To: comp.lang.java.gui
Volker Raum <Volker.Raum@heitec.de> wrote in
news:g5k5mq$o58$1@murphy.mediascape.de:
Hi all,
i am looking for a way to register a listener to be informed whenever
a window (Dialog) is opened in my application.
I do not want to keep track in may placec for that.
I am interested in a central place.
I hope you can help me.
Greetings
Volker
Hi Volker,
You need to add an AWTEventListener to the default Toolkit.
Please see an example below.
Hope this helps.
Piet
import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class WindowMonitor {
public static void main(String[] args) {
registerGeneralWindowListener();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
for (int index = 0; index < 3; index++) {
createWindow(index);
}
}
private void createWindow(int number) {
JFrame frame = new JFrame(String.valueOf(number));
frame.setName(String.valueOf(number));
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(200, 200);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
}
private static void registerGeneralWindowListener() {
AWTEventListener listener = new AWTEventListener() {
@Override
public void eventDispatched(AWTEvent event) {
if (event instanceof WindowEvent) {
WindowEvent windowEvent = (WindowEvent) event;
System.out.println(windowEvent.getWindow().getName()
+ " event type "
+ getDescription(windowEvent.getID()));
}
}
private String getDescription(int eventID) {
switch (eventID) {
case WindowEvent.WINDOW_ACTIVATED:
return "activated";
case WindowEvent.WINDOW_CLOSED:
return "closed";
case WindowEvent.WINDOW_CLOSING:
return "closing";
case WindowEvent.WINDOW_DEACTIVATED:
return "deactivated";
case WindowEvent.WINDOW_DEICONIFIED:
return "deiconified";
case WindowEvent.WINDOW_GAINED_FOCUS:
return "gained focus";
case WindowEvent.WINDOW_ICONIFIED:
return "iconified";
case WindowEvent.WINDOW_LOST_FOCUS:
return "lost focus";
case WindowEvent.WINDOW_OPENED:
return "opened";
default:
return "undefined";
}
}
};
Toolkit.getDefaultToolkit().addAWTEventListener(listener,
AWTEvent.WINDOW_EVENT_MASK);
}
}
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24