GUI Changes: The AWT Grows Up |
Window events are generated by aWindow
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 close windows. 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
TheWindowListener
interface and its corresponding adapter class,WindowAdapter
, contain seven methods:
void windowOpened(WindowEvent)
- Called by the AWT just after the listened-to window has been shown for the first time [or shown after it was closed?].
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
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. [describe applet][applet goes here]
Try this:
- Do something.
You can find the applet's code [NOWHERE YET]. Here is the applet's window event handling code:
[code goes here]You can find more examples of window listeners in the following places:
The
WindowEvent
ClassEach window event method has a single parameter: aWindowEvent
object. TheWindowEvent
class defines a useful method,getWindow
, which returns theWindow
that generated the window event.
GUI Changes: The AWT Grows Up |