Even and Listener (Java Event Handing)
Changing the state of an object is known as an event. For example, click on button, dragging mouse etc.
Java provides java.awt.event package for event classes and Listener interfaces for event handling.
Important Event classes and Listener interfaces:
Event classes
|
Listener Interfaces
|
ActionEvent
|
ActionListener
|
MouseEvent
|
MouseListener and MouseMotionListener
|
MouseWheelEvent
|
MouseWheelListener
|
KeyEvent
|
KeyListener
|
ItemEvent
|
ItemListener
|
TextEvent
|
TextListener
|
AdjustmentEvent
|
AdjustmentListener
|
windowEvent
|
windowListener
|
ComponentEvent
|
Component Listener
|
ContainerEvent
|
ContainerListener
|
FocusEvent
|
FocusListener
|
Steps to perform Event Handling:
Consider the following two required steps to perform event handling:
1. Implement the Listener Interface and overrides its methods
2. Register the component with the Listener.
To register the component with the Listener:
Use many classes that provide the registration methods.
For example:
1. Button
public void addActionListener(ActionListener a) { }
2. MenuItem
public void addActionListener(ActionListener a) { }
3. TextField
public void addActionListener(ActionListener a) { }
public void addTextListener(TextListener a) { }
4. TextArea
public void addTextListener(TextListener a) { }
5. checkbox
public void addItemtListener(ItemListener a) { }
6. choice
public void addItemtListener(ItemListener a) { }
7. List
public void addItemtListener(ItemListener a) { }
public void addActionListener(ActionListener a) { }