Java Reference
In-Depth Information
T ABLE 16.1
User Action, Source Object, Event Type, Listener Interface, and Handler
User Action
Source Object
Event Type Fired
Listener Interface
Listener Interface Methods
JButton
ActionEvent
ActionListener
actionPerformed(ActionEvent e)
Click a button
Press Enter in a text field JTextField
ActionEvent
ActionListener
actionPerformed(ActionEvent e)
Select a new item
JComboBox
ActionEvent
ItemEvent
ActionListener
ItemListener
actionPerformed(ActionEvent e)
itemStateChanged(ItemEvent e)
Check or uncheck
JRadioButton ActionEvent
ItemEvent
ActionListener
ItemListener
actionPerformed(ActionEvent e)
itemStateChanged(ItemEvent e)
Check or uncheck
JCheckBox
ActionEvent
ItemEvent
ActionListener
ItemListener
actionPerformed(ActionEvent e)
itemStateChanged(ItemEvent e)
Select a new item
JComboBox
ActionEvent
ItemEvent
ActionListener
ItemListener
actionPerformed(ActionEvent e)
itemStateChanged(ItemEvent e)
Mouse pressed
Component
MouseEvent
MouseListener
mousePressed(MouseEvent e)
Mouse released
mouseReleased(MouseEvent e)
Mouse clicked
mouseClicked(MouseEvent e)
Mouse entered
mouseEntered(MouseEvent e)
Mouse exited
mouseExited(MouseEvent e)
Mouse moved
MouseMotionListener mouseMoved(MouseEvent e)
Mouse dragged
mouseDragged(MouseEvent e)
Key pressed
Component
KeyEvent
KeyListener
keyPressed(KeyEvent e)
Key released
keyReleased(KeyEvent e)
Key typed
keyTyped(KeyEvent e)
Note
All the event classes in Figure 16.4 are included in the java.awt.event package
except ListSelectionEvent and ChangeEvent , which are in the
javax.swing.event package. AWT events were originally designed for AWT com-
ponents, but many Swing components fire them.
16.1
What is an event source object? What is an event object? Describe the relationship
between an event source object and an event object.
Check
Point
16.2
Can a button fire a MouseEvent ? Can a button fire a KeyEvent ? Can a button fire an
ActionEvent ?
16.3 Listeners, Registrations, and Handling Events
A listener is an object that must be registered with an event source object, and it must
be an instance of an appropriate event-handling interface.
Key
Point
Java uses a delegation-based model for event handling: a source object fires an event, and an
object interested in the event handles it. The latter object is called an event listener or simply
listener . For an object to be a listener for an event on a source object, two things are needed,
as shown in Figure 16.5.
event delegation
event listener
1. The listener object must be an instance of the corresponding event-listener interface to
ensure that the listener has the correct method for processing the event. Java provides a
listener interface for every type of event. The listener interface is usually named
XListener for XEvent , with the exception of MouseMotionListener . The last
event-listener interface
XListener / XEvent
 
 
 
Search WWH ::




Custom Search