J.E.D.I.
8.4 Event Listeners
Event listeners are just classes that implement the TypeListener interfaces. The following table shows some of the listener interfaces commonly used.
Event Listeners Description
ActionListener Receives action events.
MouseListener Receives mouse events.
MouseMotionListener Receives mouse motion events, which include dragging and
moving the mouse. WindowListener
Receives window events.
Table 24: Event Listeners
8.4.1 ActionListener Method
The ActionListener interface contains only one method.
ActionListener Method
public void actionPerformedActionEvent e Contains the handler for the ActionEvent e that occurred.
Table 25: The ActionListener method
8.4.2 MouseListener Methods
These are the MouseListener methods that should be overriden by the implementing class.
MouseListener Methods
public void mouseClickedMouseEvent e Contains the handler for the event when the mouse is clicked i.e., pressed and
released. public void mouseEnteredMouseEvent e
Contains the code for handling the case wherein the mouse enters a component. public void mouseExitedMouseEvent e
Contains the code for handling the case wherein the mouse exits a component. public void mousePressedMouseEvent e
Invoked when the mouse button is pressed on a component. public void mouseReleasedMouseEvent e
Invoked when the mouse button is released on a component.
Table 26: The MouseListener methods
Introduction to Programming II Page 109
J.E.D.I.
8.4.3 MouseMotionListener Methods
The MouseMotionListener has two methods to be implemented.
MouseListener Methods
public void mouseDraggedMouseEvent e Contains the code for handling the case wherein the mouse button is pressed on a
component and dragged. Called several times as the mouse is dragged. public void mouseMovedMouseEvent e
Contains the code for handling the case wherein the mouse cursor is moved onto a component, without the mouse button being pressed. Called multiple times as the
mouse is moved.
Table 27: The MouseMotionListener methods
8.4.4 WindowListener Methods
These are the methods of the WindowListener interface.
WindowListener Methods
public void windowOpenedWindowEvent e Contains the code for handling the case when the Window object is opened i.e., made
visible for the first time. public void windowClosingWindowEvent e
Contains the code for handling the case when the user attempts to close Window object from the objects system menu.
public void windowClosedWindowEvent e Contains the code for handling the case when the Window object was closed after
calling dispose i.e., release of resources used by the source on the object. public void windowActivatedWindowEvent e
Invoked when a Window object is the active window i.e., the window in use. public void windowDeactivatedWindowEvent e
Invoked when a Window object is no longer the active window. public void windowIconifiedWindowEvent e
Called when a Window object is minimized. public void windowDeiconifiedWindowEvent e
Called when a Window object reverts from a minimized to a normal state.
Table 28: The WindowListener methods
8.4.5 Guidelines for Creating Applications Handling GUI Events