CSCE 155

Handout 9:  As the World Listens …

 

February 16, 2005

 

Listeners in Java

Package java.awt.event

Provides interfaces and classes for dealing with different types of events fired by AWT components.

 

Interface Summary

ActionListener

The listener interface for receiving action events.

AdjustmentListener

The listener interface for receiving adjustment events.

AWTEventListener

The listener interface for receiving notification of events dispatched to objects that are instances of Component or MenuComponent or their subclasses.

ComponentListener

The listener interface for receiving component events.

ContainerListener

The listener interface for receiving container events.

FocusListener

The listener interface for receiving keyboard focus events on a component.

HierarchyBoundsListener

The listener interface for receiving ancestor moved and resized events.

HierarchyListener

The listener interface for receiving hierarchy changed events.

InputMethodListener

The listener interface for receiving input method events.

ItemListener

The listener interface for receiving item events.

KeyListener

The listener interface for receiving keyboard events (keystrokes).

MouseListener

The listener interface for receiving "interesting" mouse events (press, release, click, enter, and exit) on a component.

MouseMotionListener

The listener interface for receiving mouse motion events on a component.

MouseWheelListener

The listener interface for receiving mouse wheel events on a component.

TextListener

The listener interface for receiving text events.

WindowFocusListener

The listener interface for receiving WindowEvents, including WINDOW_GAINED_FOCUS and WINDOW_LOST_FOCUS events.

WindowListener

The listener interface for receiving window events.

WindowStateListener

The listener interface for receiving window state events.

 

Example Events in Java: Action Events

java.awt.event
Interface ActionListener

public interface ActionListener

extends EventListener

The listener interface for receiving action events. The class that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component's addActionListener method. When the action event occurs, that object's actionPerformed method is invoked.

Method Summary

 void

actionPerformed(ActionEvent e)
          Invoked when an action occurs.

 

 

Example Events in Java: Mouse Events

java.awt.event
Interface MouseListener

public interface MouseListener

extends EventListener

The listener interface for receiving "interesting" mouse events (press, release, click, enter, and exit) on a component. (To track mouse moves and mouse drags, use the MouseMotionListener.)

The class that is interested in processing a mouse event either implements this interface (and all the methods it contains) or extends the abstract MouseAdapter class (overriding only the methods of interest).

The listener object created from that class is then registered with a component using the component's addMouseListener method. A mouse event is generated when the mouse is pressed, released clicked (pressed and released). A mouse event is also generated when the mouse cursor enters or leaves a component. When a mouse event occurs, the relevant method in the listener object is invoked, and the MouseEvent is passed to it.

Method Summary

 void

mouseClicked(MouseEvent e)
          Invoked when the mouse button has been clicked (pressed and released) on a component.

 void

mouseEntered(MouseEvent e)
          Invoked when the mouse enters a component.

 void

mouseExited(MouseEvent e)
          Invoked when the mouse exits a component.

 void

mousePressed(MouseEvent e)
          Invoked when a mouse button has been pressed on a component.

 void

mouseReleased(MouseEvent e)
          Invoked when a mouse button has been released on a component.

 

Further Investigation

Check out all these interfaces online and find out what types of events can be captured and handled.  The more you know, the more you realize how much you could empower a graphical user interface, to make it really interactive and responsive.

·        Based on http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ActionListener.html

·        Based on http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/MouseListener.html

·        Based on http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/package-summary.html