Advanced Java Programming · Unit 3 · 4 hrs
Event Handling
Exam-focused notes for Event Handling (Advanced Java Programming, CSC419): what the TU syllabus asks and how it has actually been tested, with 5 solved past questions from this unit.
Event Handling in short
This unit explains how a Swing or AWT program reacts to user actions such as clicks and key presses, through the delegation event model, its listener interfaces, and the adapter classes that make listeners easier to write.
What this unit covers
- Event Handling Concept, Listener Interfaces, Using Action Commands, Adapter Classes
- Handling Action Events, Key Events, Focus Events, Mouse Event, Window Event, Item Events
Handling Action Events, Key Events, Focus Events, Mouse Event, Window Event, Item Events
2079 · 5 marks
Why do we need event handling? Explain the use of action event with example. [5]
2076 · 10 marks
Write a Java program to find the sum of two numbers using swing components. Use text fields for input and output. Your program displays output if you press any key in keyboard. Use key adapter to handle events.[10]
Event Handling Concept, Listener Interfaces, Using Action Commands, Adapter Classes
2078 · 5 marks
How can we use listener interface to handle events? Compare listener interface with adapter class. [5]
2077 · 5 marks
Define event delegation model. Why do we need adapter class in event handling? [5]
Show solved answerHide answer
Event Delegation Model and Adapter Classes in Java
Event Delegation Model (2 marks)
The Event Delegation Model is the standard event handling mechanism used in Java AWT and Swing. In this model, the task of handling an event is delegated from the source (component that generates the event) to a separate object called the listener (or handler).
Key Components:
| Component | Role |
|---|---|
| Event Source | The UI component that generates the event (e.g., Button, TextField) |
| Event Object | Encapsulates information about the event (e.g., ActionEvent, KeyEvent) |
| Event Listener | The object that receives and handles the event |
How it Works:
User Action --> Event Source --> Event Object --> Event Listener --> Handler Method
(e.g., click) (Button) (ActionEvent) (ActionListener) (actionPerformed())
Steps in Event Delegation:
- Implement the appropriate listener interface (e.g.,
ActionListener,KeyListener) - Register the listener with the source using
addXxxListener(this) - Override the required handler method(s)
Example (from notes):
class AEvent extends Frame implements ActionListener {
Button b = new Button("click me");
b.addActionListener(this); // Step 2: Register listener
public void actionPerformed(ActionEvent e) { // Step 3: Handle event
tf.setText("Welcome");
}
}
Why We Need Adapter Classes (3 marks)
The Problem:
Some listener interfaces in Java contain multiple abstract methods that must ALL be overridden when implementing the interface, even if we only need one or two of them.
For example, KeyListener has 3 methods:
keyPressed(KeyEvent e)keyReleased(KeyEvent e)keyTyped(KeyEvent e)
If we implement KeyListener directly, we are forced to provide the body of all 3 methods, even if we only care about keyPressed. This leads to unnecessary, cluttered code:
// Without Adapter - must override ALL methods (messy)
class MyClass extends Frame implements KeyListener {
public void keyPressed(KeyEvent e) { /* actual code */ }
public void keyReleased(KeyEvent e) { /* empty - not needed */ }
public void keyTyped(KeyEvent e) { /* empty - not needed */ }
}
The Solution - Adapter Classes:
An Adapter class is a pre-built class that implements a listener interface with empty method bodies. We simply extend the adapter class and override only the methods we need.
// With Adapter - override ONLY what is needed (clean)
class KeyChecker extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
// only this method is needed
}
// No need to write keyReleased() or keyTyped()
}
MouseAdapter Example (from notes):
public class MouseAdapterExample extends MouseAdapter {
// Only mouseClicked() is overridden - no other methods needed
public void mouseClicked(MouseEvent e) {
Graphics g = f.getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(), e.getY(), 30, 30);
}
}
Common Adapter Classes:
| Adapter Class | Corresponding Listener Interface |
|---|---|
KeyAdapter | KeyListener |
MouseAdapter | MouseListener |
WindowAdapter | WindowListener |
FocusAdapter | FocusListener |
Summary:
Adapter classes reduce code complexity by providing default (empty) implementations of all methods in a listener interface, allowing the programmer to override only the required methods.
Make Unit 3 stick
Practice CSC419 with flashcards & quizzes