Java uses the Delegation Event Model for handling events, which separates event generation from event handling.
- Event Source:
- An object generates an event when an action occurs.
- Example: A button click generates an event.
- Event Listener:
- An object that wants to receive notifications of events.
- Must implement a listener interface and register itself with the source.
- Event Notification:
- The source notifies registered listeners by calling appropriate methods in the listener interface.
Steps for Event Handling
- Implement the Listener Interface:
- Create a class that implements one or more event listener interfaces.
- Register the Listener:
- Register the listener object with the event source using methods like addActionListener().
- Handle the Event:
- Define the logic inside the listener’s method to handle the event.
Important Event Listener Interfaces
Interface | Purpose |
ActionListener | Handles action events like button clicks. |
KeyListener | Handles keyboard events like key presses. |
MouseListener | Handles mouse click, press, release, enter, and exit events. |
MouseMotionListener | Handles mouse movement and drag events. |
WindowListener | Handles window events like opening, closing, minimizing. |
ItemListener | Handles item events like selecting a checkbox. |
FocusListener | Handles focus gained and lost events. |
Example: Handling Button Click Events
Code Example:
import java.awt.*;
import java.awt.event.*;
public class ButtonClickExample extends Frame implements ActionListener {
Button button;
// Constructor
public ButtonClickExample() {
// Create a button
button = new Button(“Click Me”);
button.setBounds(50, 100, 80, 30);
// Register the listener
button.addActionListener(this);
// Add button to frame
add(button);
// Frame settings
setSize(200, 200);
setLayout(null);
setVisible(true);
}
// Handle the button click event
@Override
public void actionPerformed(ActionEvent e) {
button.setLabel(“Clicked!”);
}
public static void main(String[] args) {
new ButtonClickExample();
}
}
Example: Handling Mouse Events
Code Example:
import java.awt.*;
import java.awt.event.*;
public class MouseEventExample extends Frame implements MouseListener {
Label label;
// Constructor
public MouseEventExample() {
// Create a label
label = new Label();
label.setBounds(20, 50, 150, 20);
// Register the listener
addMouseListener(this);
// Add label to frame
add(label);
// Frame settings
setSize(300, 200);
setLayout(null);
setVisible(true);
}
// Implement mouse listener methods
@Override
public void mouseClicked(MouseEvent e) {
label.setText(“Mouse Clicked”);
}
@Override
public void mouseEntered(MouseEvent e) {
label.setText(“Mouse Entered”);
}
@Override
public void mouseExited(MouseEvent e) {
label.setText(“Mouse Exited”);
}
@Override
public void mousePressed(MouseEvent e) {
label.setText(“Mouse Pressed”);
}
@Override
public void mouseReleased(MouseEvent e) {
label.setText(“Mouse Released”);
}
public static void main(String[] args) {
new MouseEventExample();
}
}
Advantages of Event Handling
- Separation of Concerns:
- The delegation model separates event sources and listeners, making code modular and reusable.
- Flexibility:
- Multiple listeners can handle events from a single source or vice versa.
- Scalability:
- Supports large-scale GUI applications with numerous events and interactions.
Limitations of Event Handling
- Complexity:
- Managing multiple event sources and listeners can lead to intricate code.
- Performance:
- Handling numerous events in real-time may affect performance in resource-constrained systems.
- Verbose Code:
- Requires implementing multiple interfaces, leading to boilerplate code.
Modern Alternatives
Java Swing and JavaFX provide more advanced and flexible event-handling mechanisms compared to the basic AWT event model. These frameworks are preferred for modern Java GUI applications.
Conclusion Event handling in Java is a robust mechanism that enables developers to create interactive and responsive GUI applications. By understanding the delegation event model and the key listener interfaces, developers can build scalable and user-friendly applications effectively.