Title: Mouse and Key Events
1Mouse and Key Events
- Mostly corresponds
- with
- Ch14 pp 477-482
2The Delegation Model
Event-generating Objects send Events to Listener
Objects
Each event-generating object (usually a
component) maintains a set of listeners for each
event that it generates. To be on the list, a
listener object must register itself with the
event-generating object. Listeners have
event-handling methods that respond to the event.
3Event Classes
Note input events (mouse, key) are considered to
be low-level events. Any component can generate
low-level events, and they do not capture meaning
of the component. This is contrasted with
high-level events (such as ActionEvent) which are
semantic in that they convey some meaning of the
nature of the event.
4An example of Mouse event handling
MouseListener methods
MouseMotionListener methods
5An example of Key event handling
Key-related listener interface
Note the wide variety of key codes that can be
identified by the KeyEvent.
6AWT Event Adapter Classes
- ComponentAdapter
- ContainerAdapter
- FocusAdapter
- KeyAdapter
- MouseAdapter
- MouseMotionAdapter
All are in the java.awt.event package All are
derived from Object in the java.lang package Each
implements an associated Listener interface
Note interface implementations require that ALL
abstract methods be definedAdapters take care of
that for you in case you only want to define a
subset of the methods for a listener
7An example of Mouse event handling via a
MouseAdapter class
NOTE here, I do not implement the listener.
Instead, I create a subclass of the MouseAdapter
class, and override only the methods I need. An
instance of my subclass serves as the listener.