Java Reference
In-Depth Information
EVENT ID DESCRIPTION
WINDOW_ACTIVATED
The event that occurs when the window is activated — obtains the focus, in other
words. When another GUI component has the focus, you could make the window
obtain the focus by clicking on it, for example.
WINDOW_DEACTIVATED
The event that occurs when the window is deactivated — loses the focus, in other
words. Clicking on another window would cause this event, for example.
WINDOW_GAINED_FOCUS
The event that occurs when the window gains the focus. This implies that the win-
dow or one of its components receives keyboard events.
WINDOW_LOST_FOCUS
The event that occurs when the window loses the focus. This implies that keyboard
events are not delivered to the window or any of its components.
WINDOW_ICONIFIED
The event that occurs when the window is minimized and reduced to an icon.
WINDOW_DEICONIFIED
The event that occurs when the window is restored from an icon.
WINDOW_STATE_CHANGED
The event that occurs when the window state changes — when it is maximized or
minimized, for instance.
If any of these events occur, your
processWindowEvent()
method is called. Your version of the method
overrides the base class method that is inherited from
Window
and is responsible for passing the event to
any listeners that have been registered. The argument of type
WindowEvent
that is passed to the method
contains the event ID that identifies the event that occurred. To obtain the ID of the event, you call the
getID()
method for the event object and compare that with the ID that identifies the
WINDOW_CLOSING
event. If the event is
WINDOW_CLOSING
, you call the
dispose()
method for the window to close the win-
dow and release the system resources it is using. You then close the application by calling the
exit()
method defined in the
System
class. If you need convincing that it is your method closing the application,
put an output statement immediately before the call to exit.
NOTE
The
getID()
method is defined in the
AWTEvent
class, which is a superclass
of all the low-level event classes I have discussed, so all event objects that encapsu-
late low-level events have this method.
In the
SketcherFrame
class, the
dispose()
method is inherited from the
Window
class via the base class
JFrame
. This method releases all the resources for the window object, including those for all components
owned by the object. Calling the
dispose()
method doesn't affect the window object itself. It just tells
the operating system that the resources used to display the window and the components it contains are
no longer required. The window object is still around together with its components, so you could call its
methods or even open it again.
WARNING
Note that you call the
processWindowEvent()
method in the super-
class if it is not the closing event. It is very important that you do this as it allows
the event to be passed on to any listeners that have been registered for these events.