Trail: Creating a GUI with JFC/Swing
Lesson: Writing Event Listeners
Section: Implementing Listeners for Commonly Handled Events
How to Write Window Listeners
Home Page > Creating a GUI with JFC/Swing > Writing Event Listeners
How to Write Window Listeners
This section explains how to implement three kinds of window-related event handlers: WindowListener, WindowFocusListener, and WindowStateListener. All three listeners handle WindowEvent objects. The methods in all three event handlers are implemented by the abstract WindowAdapter class.

When the appropriate listener has been registered on a window (such as a frame or dialog), window events are fired just after the window activity or state has occurred. A window is considered as a "focus owner", if this window receives keyboard input.

The following window activities or states can precede a window event:

The Window Listener API

The window listener API consists of three window listener interfaces and the WindowEvent class. Their methods are listed in the following tables: The methods from all three interfaces are available through the WindowAdapter class.

The WindowListener Interface

Method Purpose
windowOpened(WindowEvent) Called just after the listened-to window has been shown for the first time.
windowClosing(WindowEvent) Called in response to a user request for the listened-to window to be closed. To actually close the window, the listener should invoke the window's dispose or setVisible(false) method.
windowClosed(WindowEvent) Called just after the listened-to window has closed.
windowIconified(WindowEvent)
windowDeiconified(WindowEvent)
Called just after the listened-to window is iconified or deiconified, respectively.
windowActivated(WindowEvent)
windowDeactivated(WindowEvent)
Called just after the listened-to window is activated or deactivated, respectively. These methods are not sent to windows that are not frames or dialogs. For this reason, we prefer the 1.4 windowGainedFocus and windowLostFocus methods to determine when a window gains or loses the focus.

The WindowFocusListener Interface

Method Purpose
windowGainedFocus(WindowEvent)
windowLostFocus(WindowEvent)
Called just after the listened-to window gains or loses the focus, respectively.

The WindowStateListener Interface

Method Purpose
windowStateChanged(WindowEvent) Called just after the listened-to window's state is changed by being iconified, deiconified, maximized, or returned to normal. The state is available through the WindowEvent as a bitwise mask. The possible values, defined in java.awt.Frame, are:
  • NORMAL. Indicates that no state bits are set.
  • ICONIFIED.
  • MAXIMIZED_HORIZ.
  • MAXIMIZED_VERT.
  • MAXIMIZED_BOTH. Concatenates MAXIMIZED_HORIZ and MAXIMIZED_VERT. A window manager may support MAXIMIZED_BOTH, while not supporting MAXIMIZED_HORIZ or MAXIMIZED_VERT. The java.awt.Toolkit method isFrameStateSupported(int) can be used to determine what states are supported by the window manager.

The WindowEvent Class
Method Purpose
Window getWindow() Returns the window that fired the event. You can use this instead of the getSource method.
Window getOppositeWindow() Returns the other window involved in this focus or activation change. For a WINDOW_ACTIVATED or WINDOW_GAINED_FOCUS event, this returns the window that lost activation or the focus. For a WINDOW_DEACTIVATED or WINDOW_LOST_FOCUS event, this returns the window that gained activation or the focus. For any other type of WindowEvent with a Java application in a different VM or context, or with no other window, null is returned. This method was introduced in JDK release 1.4.
int getOldState()
int getNewState()
For WINDOW_STATE_CHANGED events, these methods return the previous or new state of the window as a bitwise mask. These methods were introduced in JDK release 1.4.

Examples that Use Window Listeners

The following table lists the examples that use window listeners.

Example Where Described Notes
WindowEventDemo This section Reports all window events that occur on one window to demonstrate the circumstances under which window events are fired.
SliderDemo How to Use Sliders Listens for window iconify and deiconify events, so that it can stop the animation when the window isn't visible.
InternalFrameEventDemo How to Write an Internal Frame Listener Reports all internal frame events that occur on one internal frame to demonstrate the circumstances under which internal frame events are fired. Internal frame events are similar to window events.
DialogDemo Text Component Features CustomDialog.java uses setDefaultCloseOperation instead of a window listener to determine what action to take when the user closes the window.
Framework A demo that allows multiple windows to be created and destroyed.

Previous page: How to Write an Undoable Edit Listener
Next page: Listener API Table