Event Handling in Java
🌟 Event Handling in Java
Event Handling is the mechanism that controls the events and defines how a program responds to user interactions such as:
-
Mouse clicks
-
Keyboard input
-
Window actions (e.g., closing a window)
-
Button presses
Java uses the Delegation Event Model to handle these interactions in GUI applications.
🎯 1. Event Handling Mechanism
Java’s event handling is based on three components:
-
Event Source – the component (e.g., button) that generates the event.
-
Event Object (Event Class) – encapsulates information about the event.
-
Event Listener (Interface) – receives and processes the event.
Java’s model is known as the Delegation Event Model, where the source delegates the task of handling the event to the listener.
🔄 2. Delegation Event Model
The Delegation Event Model is Java's standard for handling events. It has three main participants:
-
Source: Component that generates events (e.g.,
JButton) -
Event Object: Encapsulates the event (e.g.,
ActionEvent) -
Listener: Interface that must be implemented to handle the event (e.g.,
ActionListener)
🔧 Steps in the Delegation Model:
-
A component is registered with an event listener using methods like
.addActionListener(). -
When the event occurs, the component delegates the event to the listener.
-
The listener processes the event by executing the code in its method (e.g.,
actionPerformed()).
📦 3. Event Classes (Event Objects)
Java provides a set of event classes in the java.awt.event and javax.swing.event packages.
| Event Class | Description |
|---|---|
ActionEvent | Generated when a button is clicked |
MouseEvent | Mouse movements and clicks |
KeyEvent | Keyboard key press/release |
WindowEvent | Window open/close/minimize |
ItemEvent | Checkbox or radio button selection change |
AdjustmentEvent | Scrollbar adjustments |
TextEvent | Text field changes |
FocusEvent | When a component gains/loses focus |
💡 4. Event Sources
Sources are components like:
-
Button,JButton -
TextField,JTextField -
Checkbox,JCheckBox -
Window,JFrame,JDialog
Each source has methods like:
-
addActionListener() -
addMouseListener() -
addKeyListener()
🎧 5. Event Listener Interfaces
Java provides listener interfaces that must be implemented to handle events. Here are some commonly used listeners:
| Listener Interface | Method to Implement | Used With |
|---|---|---|
ActionListener | actionPerformed(ActionEvent e) | Buttons, menus |
MouseListener | mouseClicked(), mousePressed(), etc. | Mouse events |
KeyListener | keyPressed(), keyReleased(), etc. | Keyboard |
ItemListener | itemStateChanged(ItemEvent e) | Checkbox, combo box |
WindowListener | windowClosing(), etc. | Windows |
FocusListener | focusGained(), focusLost() | Components with focus |
🛠️ 6. Using the Delegation Event Model – Example
🔍 How It Works:
-
JButtonis the event source. -
ActionEventis the event object. -
The class implements
ActionListenerand overridesactionPerformed(). -
The listener is registered using
addActionListener(this).
✅ Summary Table
| Component | Role |
|---|---|
| Event Source | Triggers event (e.g., Button) |
| Event Object | Stores event details (e.g., ActionEvent) |
| Event Listener | Responds to event (e.g., ActionListener) |
| Delegation Model | Source delegates event to registered listener |
Comments
Post a Comment