Using Components, the GUI Building Blocks |
Window events are generated by aWindow
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.
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
TheWindowListener
interface and its corresponding adapter class,WindowAdapter
, 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
orsetVisible(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:
- 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.- Click the window if it doesn't already have the focus.
Do you see a "Window activated" message in the applet's display area?- Iconify the window, using the window controls.
You'll see a "Window iconified" message in the applet's display area.- Deiconify the window.
You'll see a "Window deiconified" message in the applet's display area.- Close the window, using the window controls.
You'll see "Window closing" in the applet's display area. Because the window-closing event handler invokessetVisible(false)
instead ofdispose()
, 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
ClassEach window event method has a single parameter: aWindowEvent
object. TheWindowEvent
class defines one useful method,getWindow
, which returns theWindow
that generated the window event.
Using Components, the GUI Building Blocks |