Java Reference
In-Depth Information
If you don't call
processWindowEvent()
for the superclass, any events that you do
not handle in your
processWindowEvent()
method are lost.
If you had not removed the call to the
setDefaultCloseOperation()
method, your
processWin-
dowEvent()
method would still have been called when the close icon was clicked. with the call to
setDefaultCloseOperation()
in effect, you would not need to call
dispose()
and
exit()
in the
method. This would all have been taken care of automatically after your
processWindowEvent()
meth-
od had finished executing. This would be preferable as it means there would be less code in your pro-
gram, and the code to handle the default close action is there in the
JFrame
class anyway.
Enabling Other Low-Level Events
The
enableEvents()
method is inherited from the
Component
class. This means that any component can
elect to handle its own events. You just call the
enableEvents()
method for the component and pass an
argument defining the events you want the component to handle. If you want to enable more than one type
of event for a component, you just combine the event masks from
AWTEvent
that you saw earlier by linking
them with a bitwise OR. To make the window object handle mouse events as well as window events, you
could write the following:
enableEvents(WINDOW_EVENT_MASK | MOUSE_EVENT_MASK);
Of course, you must now also implement the
processMouseEvent()
method for the
SketcherFrame
class.
Like the
processWindowEvent()
method, this method is protected and has
void
as the return type. It re-
ceives the event as an argument of type
MouseEvent
. There are two other methods specific to the
Window
class that handle events, shown in
Table 18-3
.
TABLE 18-3
:
Window Class Methods for Handling Window Events
EVENT-HANDLING METHODS
DESCRIPTION
This is called for any window focus events that arise as long as such
events are enabled for the window.
processWindowFocusEvent(WindowEvent
e)
This is called for events resulting from the window changing state.
processWindowStateEvent(WindowEvent
e)
These methods and the
processWindowEvent()
method are available only for objects of type
Window
or
a subclass of
Window
, so don't try to enable window events on other components.
The other event-handling methods in the window that you can override to handle component events are
show in
Table 18-4
.
TABLE 18-4
:
Window Class Methods for Handling Component Events
EVENT-HANDLING METHODS
DESCRIPTION
This is called first for any events that are enabled for the component. If
you implement this and fail to call the base class method, none of the
methods for specific groups of events are called.
processEvent(AWTEvent e)
This is called for focus events, if they are enabled for the component.
processFocusEvent(FocusEvent e)
This is called for key events, if they are enabled for the component.
processKeyEvent(KeyEvent e)