Java Reference
In-Depth Information
EVENT-HANDLING METHODS DESCRIPTION
processMouseEvent(MouseEvent e)
This is called for mouse button events, if they are enabled for the com-
ponent.
processMouseMotionEvent(MouseEvent e)
This is called for mouse move and drag events, if they are enabled for
the component.
This is called for mouse wheel rotation events, if they are enabled for
the component.
processMouseWheelEvent(MouseWheelEvent
e)
All the event-handling methods for a component are protected methods that have a return type of
void
.
The default behavior implemented by these methods is to dispatch the events to any listeners registered for
the component. If you don't call the base class method when you override these methods, this behavior is
lost.
WARNING
Although it seems convenient to handle the window-closing event in the
Sketch-
erFrame
class by implementing
processWindowEvent()
, as a general rule you should use
listeners to handle events. Using listeners is the recommended approach to handling events in
the majority of circumstances. Separating the event handling from the object that originated
the event results in a simpler code structure that is easier to understand and is less error
prone. You change the handling of the window-closing event in Sketcher to use a listener later
in this chapter.
Low-Level Event Listeners
A class that defines an event listener must implement a listener interface. All event listener interfaces extend
the interface
java.util.EventListener
. This interface doesn't declare any methods, though — it's just
used to identify event listener objects. You can use a variable of type
EventListener
to hold a reference to
any kind of event listener object.
There are many event listener interfaces. You consider just eight at this point concerned with low-level
events. The following sections describe these interfaces and the methods they declare.
The WindowListener Interface
The
WindowListener
interface declares methods to respond to events reflecting changes in the state of a
window (shown in
Table 18-5
)
.
TABLE 18-5
:
WindowListener Methods
METHOD DESCRIPTION
windowOpened(WindowEvent e)
Called the first time a window is opened.
windowClosing(WindowEvent e)
Called when the system menu Close item or the window close icon is selected.
windowClosed(WindowEvent e)
Called when a window has been closed.
windowActivated(WindowEvent e)
Called when a window is activated — by clicking on it, for example.
windowDeactivated(WindowEvent
e)
Called when a window is deactivated — by clicking on another window, for ex-
ample.
windowIconified(WindowEvent e)
Called when a window is minimized and reduced to an icon.