Return to Course Content Home

Events and Event Listeners

Required Reading

We will be using the Java Tutorial on Swing Events. In particular we will be reading the sections on:

The Java Event Listener Pattern

You probably noticed that the tutorial was on Swing event listeners. Don't get fooled, event listeners are a design pattern that is used throughout Java SE and Java EE, they are not limited to GUI components. I think you'll find however that GUI components are excellent candidates for learning the event handling design pattern.

Event Handling in Java
Event Handling

In many ways, event handling is a very simple mechanism. It starts by having an Event Processor/Component that processes events (again, this can be a GUI component or a JavaEE component). The component that processes the event first maintains a list of "registered" objects that implement the particular interface that is associated with the event.

Each event contains methods to pull out whatever meaningful information there might be on that event. Simple events may not use anything more than getSource() to get the source of the event. More complex events may have several methods to pull out pertinent information.

As you saw in the required reading, ActionEvents are one of the more basic, and the easiest events to deal with.

To give you a different "take" on an ActionListener, let's walk through the process of deciding to use a JButton in a component.

Step 1: See what listeners the JButton supports.

The first thing is to go to the JDK API and take a look at the JButton object. Do you see any methods that have the text pattern addXXXListener, where XXX is the type of listener we are looking for?

Rats...no such luck, well, the next step is to look at the Methods inherited from class javax.swing.AbstractButton section. Ah...here we see lots of addXXXListener methods. Go to the AbstractButton API definition and take a look at the listener method defined in the Method Summary section. There are actually three listeners available. (Property, Action and Change), but the Action listener seems the best candidate. For other classes you may need to click on the ActionListener (or its equivalent) interface to read more about what the listener does. In this case, we'll use the ActionListener interface.

JButton's addActionListener method
JButton.addActionListner

Step 2: create an object that implements the listener interface you are interested in

For our JButton example, this is the ActionListener interface. If we look at the ActionListener interface we see a lone method that needs to be implemented.

ActionListener methods
ActionListener methods

This is the method that is called whenever the event that you are interested in happens. In JButton's case, whenever it is pushed, JButton will call this method in whatever class that registered for listening to this event.

There are many schemes to implement listeners. Some of the most common ones are

  1. The component/container that holds the object you want to watch implements the interface
  2. You have an inner class in the component/container that holds the object, and that inner class implements the interface
  3. You have a separate class that implements the interface
  4. You use an anonymous class to implement the interface

Be patient with me, we will cover some basic Swing components next, but in the case of our button example, we are going to have a single JFrame, that holds a button. In this case, the JButton in an internal attribute of the JFrame, which will act as the container for both.

So, I'm going to have something like this in my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyButton extends JFrame implements ActionListener {

  private final JButton myButton = new JButton("Press me");

  public class MyButton() {
    ...
  }

  public void actionPerformed(ActionEvent evt) {
    System.exit(0);
  }

}

The class MyButton is a JFrame that implements the ActionListener interface. That means I have to have a method called public void actionPerformed(ActionEvent evt) in my MyButton class.

In this case, when push the button, I want the application to close. Normally you won't be doing something this extreme, but it works well for our test case here.

Step 3: Register the object implementing the Listener interface with the Event Processing object

In this case, the Event Processing object is the JButton. It receives the Button Pushed event and notifies any listeners that an ActionEvent occurred. We register the Listener by adding it to the Event Processing object using the addActionListener() method (see the code highlighted in red below).

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyButton extends JFrame implements ActionListener {

  private final JButton myButton = new JButton("Press me");

  public class MyButton() {
    myButton.addActionListener(this);
  }

  public void actionPerformed(ActionEvent evt) {
    System.exit(0);
  }

}

Results

After you have performed these three steps with a listener, you have "hooked up" the wiring between an event generating component and the code you want executed.

These three steps apply to any event listener. You'll do the same steps if you are writing a listener for a Spring Java EE framework.

Practice

As a means of showing several different listeners, an several ways to implement the listeners, you can download the Eclipse project events.

This project uses a MouseListener, an ActionListener, and a WindowListener. The idea was to show as much as I could about the way events can be used.

Rather than do a lot of screen captures, I've put a lot of my comments into the code itself. When you run the application, you'll first see the screen below:

events-1

When your mouse enters the window, the status label at the top changes:

events-2

Likewise when your mouse goes to the HelloWorld button.

events-3

When you click on the HelloWorld button, you get the following wonderful message:

events-4

Lastly, to quit the app, click on the exit button, or click on the close window Icon at the top right of the window.

Summary

So, even though there are plenty of listeners in Java, they all use the same design pattern. The trick sometimes is to locate the listener you want to use, and the best way to do that is to read the API entries on the listeners.

 

 

f listeners in Java, they all use the same design pattern. The trick sometimes is to locate the listener you want to use, and the best way to do that is to read the API entries on the listeners.