2077 5

CSC419 ยท 2077 ยท 5 marks

Define event delegation model. Why do we need adapter class in event handling?

Define event delegation model. Why do we need adapter class in event handling? [5]

Advanced Java ProgrammingChapter 3: Event HandlingEvent Handling Concept, Listener Interfaces, Using Action Co

Quick answer

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).

Event Delegation Model and Adapter Classes in Java

Event Delegation Model (2 marks)

Key Components:

ComponentRole
Event SourceThe UI component that generates the event (e.g., Button, TextField)
Event ObjectEncapsulates information about the event (e.g., ActionEvent, KeyEvent)
Event ListenerThe 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:

  1. Implement the appropriate listener interface (e.g., ActionListener, KeyListener)
  2. Register the listener with the source using addXxxListener(this)
  3. 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 ClassCorresponding Listener Interface
KeyAdapterKeyListener
MouseAdapterMouseListener
WindowAdapterWindowListener
FocusAdapterFocusListener

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.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.