Previous | Next | Trail Map | To 1.1 -- And Beyond! | GUI Changes: The AWT Grows Up


Introduction to the New AWT Event Model

In the new event model, events are generated by event sources. One or more listeners can register to be notified about events of a particular kind from a particular source. Sometimes this model is called delegation, since it allows the programmer to delegate authority for event handling to any object that implements the appropriate listener interface. The new AWT event model lets you both handle and generate AWT events.

Event handlers can be instances of any class. As long as a class implements an event listener interface, its instances can handle events. In every program that has an event handler, you'll see three bits of code:

  1. In the class statement of the event handler, code declaring that the class implements a listener interface (or extends a class that implements a listener interface). For example:
    public class MyClass implements ActionListener {
         

  2. Code that registers an instance of the event handling class as a listener upon one or more components. For example:
    someComponent.addActionListener(instanceOfMyClass);
         

  3. The implementation of the methods in the listener interface. For example:
    public void actionPerformed(ActionEvent e) {
        ...//code that reacts to the action...
    }
         

A Simple Example

Here is a bare-bones 1.1 applet that illustrates event handling. It contains a single button that beeps when you click it.

Since you can't run the applet, here's a picture of it:

You can find the entire program in Beeper.java. Here's just the code that implements the event handling for the button:

public class Beeper ... implements ActionListener {
    ...
    //where initialization occurs:
        button.addActionListener(this);
    ...
    public void actionPerformed(ActionEvent e) {
        ...//Make a beep sound...
    }
}
Isn't that simple? The Beeper class implements the ActionListener interface, which contains one method: actionPerformed. Since Beeper implements ActionListener, a Beeper object can register as a listener for the action events that buttons generate. Once the Beeper has been registered using the Button addActionListener method, the Beeper's actionPerformed method is called every time the button is clicked.

A More Complex Example

The 1.1 event model, which you saw at its simplest in the above example, is quite powerful and flexible. Any number of event listener objects can listen for all kinds of events from any number of event source objects. For example, a program might create one listener per event source. Or a program might have a single listener for all events from all sources. A program can even have more than one listener for a single kind of event from a single event source.

The following applet gives an example of using multiple listeners per object. The applet contains two event sources (Button instances) and two event listeners. One of the event listeners (an instance of a class called MultiListener) listens for events from both buttons. When it receives an event, it adds the event's "action command" (the text on the button's label) to the top text area. The second event listener (an instance of a class called Eavesdropper) listens for events on only one of the buttons. When it receives an event, it adds the action command to the bottom text area.

Since you can't run the applet, here's a picture of it:

You can find the entire program in MultiListener.java. Here's just the code that implements the event handling for the button:

public class MultiListener ... implements ActionListener {
    ...
    //where initialization occurs:
        button1.addActionListener(this);
        button2.addActionListener(this);

        button2.addActionListener(new Eavesdropper(bottomTextArea));
    }

    public void actionPerformed(ActionEvent e) {
        topTextArea.append(e.getActionCommand() + "\n");
    }
}

class Eavesdropper implements ActionListener {
    ...
    public void actionPerformed(ActionEvent e) {
        myTextArea.append(e.getActionCommand() + "\n");
    }
}
In the above code, both MultiListener and Eavesdropper implement the ActionListener interface and register as action listeners using the Button addActionListener method. Both classes' implementations of the actionPerformed method are similar: they simply add the event's action command to a text area.

An Example of Handling Another Event Type

So far, the only kind of event you've seen has been action events. Let's take a look at a program that handles another kind of event: mouse events.

The following applet displays a rectangle-edged area and a text area. Whenever a mouse event -- a click, press, release, enter, or exit -- occurs on either the rectangle-edged area (BlankAreaMouseDemo), the text area displays a string describing the event.

Since you can't run the applet, here's a picture of it:

You can find the entire program in MouseDemo.java. Here's just the code that implements the event handling:

public class MouseDemo ...  implements MouseListener {
    ...
    //where initialization occurs:
        //Register for mouse events on blankArea and applet (panel).
        blankArea.addMouseListener(this);
        addMouseListener(this);
    }

    public void mousePressed(MouseEvent e) {
       saySomething("Mouse button press", e);
    }

    public void mouseReleased(MouseEvent e) {
       saySomething("Mouse button release", e);
    }

    public void mouseEntered(MouseEvent e) {
       saySomething("Cursor enter", e);
    }

    public void mouseExited(MouseEvent e) {
       saySomething("Cursor exit", e);
    }

    public void mouseClicked(MouseEvent e) {
       saySomething("Mouse button click", e);
    }

    void saySomething(String eventDescription, MouseEvent e) {
        textArea.append(eventDescription + " detected on "
                        + e.getComponent().getClass().getName()
                        + ".\n");
        textArea.setCaretPosition(maxInt); //hack to scroll to bottom
    }
}
You'll see the code explained in Implementing a Mouse Listener, later in this section.


Previous | Next | Trail Map | To 1.1 -- And Beyond! | GUI Changes: The AWT Grows Up