How to Write an Internal Frame Listener

How to Write an Internal Frame Listener

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).

[11] InternalFrameListener API documentation:

The application shown in Figure demonstrates internal frame events. The application listens for internal frame events from the Event Generator frame, displaying a message that describes each event.

Figure. The InternalFrameEventDemo application.


Try This:

  1. graphics/cd_icon.gif

    Run InternalFrameEventDemo using Java Web Start or compile and run the example yourself.[12]

    [12] To run InternalFrameEventDemo using Java Web Start, click the InternalFrameEventDemo link on the RunExamples/events.html page on the CD. You can find the source files here: JavaTutorial/uiswing/events/example-1dot4/index.html#InternalFrameEventDemo.

  2. Bring up the Event Generator internal frame by clicking the Show internal frame button. You should see an "Internal frame opened" message in the display area.

  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 How to Write Window Listeners (page 723) for information on what kinds of events you'll see.

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.










The Internal Frame Listener API

Figure lists the methods in the InternalFrameListener interface; its corresponding adapter class is InternalFrameAdapter.[13] Also refer to the InternalFrameListener API documentation at:

[13] InternalFrameAdapter API documentation:

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.

[14] InternalFrameEvent API documentation:

The InternalFrameListener Interface





Called just after the listened-to internal frame has been shown for the first time.



Called 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. 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.



Called just after the listened-to internal frame has been disposed of.





Called just after the listened-to internal frame is iconified or deiconified, respectively.





Called just after the listened-to internal frame is activated or deactivated, respectively.

Examples That Use Internal Frame Listeners

Just one of this book's examples uses internal frame listeners. However, internal frame listeners are similar to WindowListeners, which several Swing programs have.


Where Described



This section

Reports all internal frame events that occur on one internal frame to demonstrate the circumstances under which internal frame events are fired.


Using Text Components (page 60) uses setDefaultClose-Operation and not a window listener to determine what action to take when a window closes.


How to Use Sliders (page 348)

Listens for window iconify and deiconify events, so that it stops animation when the window isn't visible.

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows