CSCE
155
Handout 9: As the World Listens …
February 16, 2005
Listeners in Java
Provides interfaces and classes for dealing with different types of events fired by AWT components.
|
Interface Summary |
|
|
The listener interface for receiving action events. |
|
|
The listener interface for receiving adjustment events. |
|
|
The listener interface for receiving notification of events dispatched to objects that are instances of Component or MenuComponent or their subclasses. |
|
|
The listener interface for receiving component events. |
|
|
The listener interface for receiving container events. |
|
|
The listener interface for receiving keyboard focus events on a component. |
|
|
The listener interface for receiving ancestor moved and resized events. |
|
|
The listener interface for receiving hierarchy changed events. |
|
|
The listener interface for receiving input method events. |
|
|
The listener interface for receiving item events. |
|
|
The listener interface for receiving keyboard events (keystrokes). |
|
|
The listener interface for receiving "interesting" mouse events (press, release, click, enter, and exit) on a component. |
|
|
The listener interface for receiving mouse motion events on a component. |
|
|
The listener interface for receiving mouse wheel events on a component. |
|
|
The listener interface for receiving text events. |
|
|
The listener interface for receiving |
|
|
The listener interface for receiving window events. |
|
|
The listener interface for receiving window state events. |
|
Example Events in Java:
Action Events
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 |
|
|
|
|
Example Events in Java:
Mouse Events
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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