Previous | Next | Trail Map | Creating a User Interface (with Swing) | Using Components, the GUI Building Blocks

Writing a Window Listener

Window events are generated by a Window just after the window is opened, closed, iconified, deiconified, activated, or deactivated. Window events also notify the listener when the window has received a request that it close. Opening a window means showing it for the first time; closing it means disposing of the window. Iconifying it means substituting a small icon on the desktop for the window; deiconifying means the opposite. A window is activated if it or a component it contains has the keyboard focus; deactivation occurs when the window and all of its contents lose the keyboard focus.

The most common use of window listeners is closing windows. If a program doesn't handle window-closing events, then nothing happens when the user attempts to close a window. An application that features a single window might react to window-closing events from that window by exiting. Other programs usually react to window-closing events by disposing of the window or making it invisible.


Note: If you're using the Swing JFrame class, you might not need to write a window listener. See How to Make Frames(in the Creating a User Interface trail) for information on how the JFrame class provides automatic handling of window-closing events.

Another common use of window listeners is to stop threads and release resources when a window is iconified, and to start up again when the window is deiconified. This way, you can avoid unnecessarily using the processor or other resources. For example, when a window that contains animation is iconified, it should stop its animation thread and free any large buffers. When the window is deiconified, it can start the thread again and recreate the buffers.

If you want to be notified when a window is made visible or hidden, then you should register a component listener on the window.

Window Event Methods

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

void windowOpened(WindowEvent)
Called by the AWT just after the listened-to window has been shown for the first time.

void windowClosing(WindowEvent)
Called by the AWT in response to a user request that the listened-to window be closed. To actually close the window, the listener should invoke the window's dispose or setVisible(false) method.

void windowClosed(WindowEvent)
Called by the AWT just after the listened-to window has closed.

void windowIconified(WindowEvent)
void windowDeiconified(WindowEvent)
Called by the AWT just after the listened-to window is iconified or deiconified, respectively.

void windowActivated(WindowEvent)
void windowDeactivated(WindowEvent)
Called by the AWT just after the listened-to window is activated or deactivated, respectively.

Examples of Handling Window Events

The following applet demonstrates window events. By clicking the top button in the applet, you can bring up a small window. The controlling class listens for window events from the window, printing a message whenever it detects a window event. You can find the applet's code in WindowEventDemo.java.


Try this:
  1. Bring up the Window Demo Window by clicking the applet's top button.
    The first time you click this button, you'll see a "Window opened" message in the applet's display area.
  2. Click the window if it doesn't already have the focus.
    Do you see a "Window activated" message in the applet's display area?
  3. Iconify the window, using the window controls.
    You'll see a "Window iconified" message in the applet's display area.
  4. Deiconify the window.
    You'll see a "Window deiconified" message in the applet's display area.
  5. Close the window, using the window controls.
    You'll see "Window closing" in the applet's display area. Because the window-closing event handler invokes setVisible(false) instead of dispose(), you won't see "Window closed".

Here is the applet's window event handling code:

public class WindowEventDemo ... implements WindowListener {
     ...//where initialization occurs:
        //Create but don't show window.
        window = new Frame("Window Event Window");
        window.addWindowListener(this);
        window.add("Center",
                   new Label("The applet listens to this window"
                             " for window events."));
        window.pack();
    }

    public void windowClosing(WindowEvent e) {
            window.setVisible(false);
        displayMessage("Window closing", e);
    }

    public void windowClosed(WindowEvent e) {
        displayMessage("Window closed", e);
    }

    public void windowOpened(WindowEvent e) {
        displayMessage("Window opened", e);
    }

    public void windowIconified(WindowEvent e) {
        displayMessage("Window iconified", e);
    }

    public void windowDeiconified(WindowEvent e) {
        displayMessage("Window deiconified", e);
    }

    public void windowActivated(WindowEvent e) {
        displayMessage("Window activated", e);
    }

    public void windowDeactivated(WindowEvent e) {
        displayMessage("Window deactivated", e);
    }

    void displayMessage(String prefix, WindowEvent e) {
        display.append(prefix
                       + ": "
                       + e.getWindow()
                       + newline); 
    }
    ...
}

Here are some of the source files that contain window listeners:

The WindowEvent Class

Each window event method has a single parameter: a WindowEvent(in the API reference documentation) object. The WindowEvent class defines one useful method, getWindow, which returns the Window that generated the window event.


Previous | Next | Trail Map | Creating a User Interface (with Swing) | Using Components, the GUI Building Blocks