Previous | Next | Trail Map | To 1.1 -- And Beyond! | GUI Changes: The AWT Grows Up


Writing a Window Listener

Window events are generated by a Window just after the window is opened, closed, iconified, deiconified, activated, or deactivated. Opening a window means making it visible on-screen [CHECK]; closing means removing the window from the screen. Iconifying 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 or one of its contents loses the keyboard focus.

Probably the most common use of window listeners is to react appropriately when the user attempts to close a window. If a program doesn't handle window closing events, then nothing happens when the user attempts to close a window. An application that consists of a single window might react to window closing events by exiting. An applet or other program that exists in more than one window usually calls the window's dispose method, which closes the window.

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, a program that performs a continuous animation is useless when its window isn't visible, so it shouldn't take up system resources when it's iconified. Specifically, it should stop its animation thread and free any large buffers when its window is iconified, and start the thread again and recreate the buffers when the window is deiconified.

Window Event Methods

The WindowListener(in the API reference documentation) interface and its corresponding adapter class, WindowAdapter(in the API reference documentation), contain seven methods:
void windowOpened(WindowEvent)
Notifies the listener that the window has been shown for the first time [or after it was closed?].
void windowClosing(WindowEvent)
Notifies the listener that the user has requested that the window be closed. To close the window, the listener should invoke the window's dispose method.
void windowClosed(WindowEvent)
Notifies the listener that the window has closed.
void windowIconified(WindowEvent)
void windowDeiconified(WindowEvent)
Notifies the listener that the window has been iconified or deiconified, respectively.
void windowActivated(WindowEvent)
void windowDeactivated(WindowEvent)
Notifies the listener that the window has been activated or deactivated, respectively.

Examples of Handling Window Events

The following applet demonstrates window events. [describe applet]

[applet goes here]


Try this:
  1. Do something.

You can find the applet's code in WindowDemo.java. Here is the applet's window event handling code:

[code goes here]

You can find more examples of window listeners in the following sections: [LIST GOES HERE]

The WindowEvent Class

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


Previous | Next | Trail Map | To 1.1 -- And Beyond! | GUI Changes: The AWT Grows Up