< Day Day Up > |
An InternalFrameListener [11] is similar to a WindowListener . Like the window listener, the internal frame listener listens for events that occur when the "window" has been shown for the first time, disposed of, iconified , deiconified, activated, or deactivated. Before using an internal frame listener, please familiarize yourself with the WindowListener interface in How to Write Window Listeners (page 723).
The application shown in Figure 6 demonstrates internal frame events. The application listens for internal frame events from the Event Generator frame, displaying a message that describes each event. Figure 6. The InternalFrameEventDemo application.
Try This:
Here's the internal frame event-handling code: public class InternalFrameEventDemo ... implements InternalFrameListener ... { ... public void internalFrameClosing(InternalFrameEvent e) { displayMessage("Internal frame closing", e); } public void internalFrameClosed(InternalFrameEvent e) { displayMessage("Internal frame closed", e); listenedToWindow = null; } public void internalFrameOpened(InternalFrameEvent e) { displayMessage("Internal frame opened", e); } public void internalFrameIconified(InternalFrameEvent e) { displayMessage("Internal frame iconified", e); } public void internalFrameDeiconified(InternalFrameEvent e) { displayMessage("Internal frame deiconified", e); } public void internalFrameActivated(InternalFrameEvent e) { displayMessage("Internal frame activated", e); } public void internalFrameDeactivated(InternalFrameEvent e) { displayMessage("Internal frame deactivated", e); } void displayMessage(String prefix, InternalFrameEvent e) { String s = prefix + ": " + e.getSource(); display.append(s + newline); } public void actionPerformed(ActionEvent e) { if (SHOW.equals(e.getActionCommand())) { ... if (listenedToWindow == null) { listenedToWindow = new JInternalFrame("Event Generator", true, //resizable true, //closable true, //maximizable true); //iconifiable //We want to reuse the internal frame, so we need to //make it hide (instead of being disposed of, which is //the default) when the user closes it. listenedToWindow.setDefaultCloseOperation( WindowConstants.HIDE_ON_CLOSE); listenedToWindow.addInternalFrameListener(this); ... } } ... } } The Internal Frame Listener APITable 15 lists the methods in the InternalFrameListener interface; its corresponding adapter class is InternalFrameAdapter . [13] Also refer to the InternalFrameListener API documentation at: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/event/InternalFrameListener.html.
Each internal frame event method has a single parameter: an InternalFrameEvent [14] object. The InternalFrameEvent class defines no generally useful methods. To get the internal frame that fired the event, use the getSource method, which InternalFrameEvent inherits from java.util.EventObject .
Table 15. The InternalFrameListener Interface
Examples That Use Internal Frame ListenersJust one of this book's examples uses internal frame listeners. However, internal frame listeners are similar to WindowListener s, which several Swing programs have.
|
< Day Day Up > |