Previous | Next | Trail Map | Creating a User Interface (with Swing) | Using the JFC/Swing Packages

How to Write an Internal Frame Listener


Note: This section assumes that you're familiar with the AWT event listener scheme. If you aren't, you can read about it in The 1.1 AWT Event Model(in the Creating a User Interface trail)
Internal frame events are to JInternalFrame what window events are to JFrame. Like window events, internal frame events notify listeners that the "window" has been shown for the first time, disposed of, iconified, deiconified, activated, or deactivated. Before using internal frame events, please familiarize yourself with Writing a Window Listener(in the Creating a User Interface trail).

Internal Frame Event Methods

The InternalFrameListener(in the API reference documentation) interface and its corresponding adapter class, InternalFrameAdapter(in the API reference documentation), contain these methods:

void internalFrameOpened(InternalFrameEvent)
Called by the AWT just after the listened-to internal frame has been shown for the first time.

void internalFrameClosing(InternalFrameEvent)
Called by the AWT in response to a user request that the listened-to internal frame be closed. By default, JInternalFrame hides the window when the user closes it. You can use the JInternalFrame setDefaultCloseOperation method to specify another option, which must be either DISPOSE_ON_CLOSE or DO_NOTHING_ON_CLOSE (both defined in WindowConstants, an interface that JInternalFrame implements). Or by implementing an internalFrameClosing method in the internal frame's listener, you can add custom behavior (such as bringing up dialogs or saving data) to internal frame closing.

void internalFrameClosed(InternalFrameEvent)
Called by the AWT just after the listened-to internal frame has been disposed of.

void internalFrameIconified(InternalFrameEvent)
void internalFrameDeiconified(InternalFrameEvent)
Called by the AWT just after the listened-to internal frame is iconified or deiconified, respectively.

void internalFrameActivated(InternalFrameEvent)
void internalFrameDeactivated(InternalFrameEvent)
Called by the AWT just after the listened-to internal frame is activated or deactivated, respectively.

Examples of Handling InternalFrameEvents

The application shown in the following figure demonstrates internal frame events. The application listens for internal frame events from the Event Generator frame, displaying a message that describes each event.
Screendump of InternalFrameEventDemo

Try this:
  1. Compile and run InternalFrameEventDemo. The source file is InternalFrameEventDemo.java.
    See Getting Started with Swing if you need help.
  2. Bring up the Event Generator internal frame by clicking the applet's top button.
    You should see an "Internal frame opened" message in the display area [PENDING: but you don't!].
  3. Try various operations to see what happens. For example, click the Event Generator so that it gets activated. Click the Event Watcher so that the Event Generator gets deactivated. Click the Event Generator's decorations to iconify, maximize, minimize, and close the window.
    See Writing a Window Listener(in the Creating a User Interface trail) for information on what kinds of events you'll see.

Here is the internal frame event handling code:
public class InternalFrameEventDemo ...
		     implements InternalFrameListener ... {
    ...
    protected void createListenedToWindow() {
	listenedToWindow = new JInternalFrame("Event Generator",
					      true,  //resizable
					      true,  //closable
					      true,  //maximizable
					      true); //iconifiable
	listenedToWindow.setDefaultCloseOperation(
				WindowConstants.DISPOSE_ON_CLOSE);
	...
    }

    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 (e.getActionCommand().equals(SHOW)) {
	    ...
	    if (listenedToWindow == null) {
	        createListenedToWindow();
	        listenedToWindow.addInternalFrameListener(this);
	        ...
	    }
	} 
	...
    }
}

No other source files currently contain internal frame listeners. However, internal frame listeners are very similar to window listeners, and many Swing programs have window listeners:

Example Where Described Notes
FrameDemo.java How to Make Frames One of many examples that listens for window closing events, so that the application can exit when its only window is closed.
SliderDemo.java How to Use Sliders Listens for window iconify and deiconify events, so that it can stop the animation when the window isn't visible.

The InternalFrameEvent Class

Each internal frame event method has a single parameter: a InternalFrameEvent(in the API reference documentation) object. The InternalFrameEvent class defines no generally useful methods. To get the internal frame that generated the event, use the getSource method, which InternalFrameEvent inherits from EventObject.


Previous | Next | Trail Map | Creating a User Interface (with Swing) | Using the JFC/Swing Packages