Next: None. Up: Graphical user interfaces. Previous: Interfaces.


Event listeners

Textbook: Section 12.3

Creating a listener

So how can we have our button do something? We're going to have to use an interface - namely, the ActionListener class in the java.awt.event package, which includes the following method.

public void actionPerformed(ActionEvent evt)
Called when an ``action'' is performed on a GUI component. In the case of a button, the ``action'' occurs when the user clicks on it.

The ActionEvent paremeter specifies information about the event that has occurred. For the moment, we're just going to worry about its getSource() method, which returns the Component on which the action event was performed.

had an action

In order to define what to do for the button, we're going to need an object that implement this interface. Inside that object's actionPerformed() method, we'll have some code that say what to do.

Registering a listener

The other step in setting up the program to handle events is to register the ActionListener object with the component. In our case, we'll want the addActionListener() method in the JButton class. It takes takes an ActionListener as an argument and sets up the button so that, when clicked, it calls the actionPerformed() method in that ActionListener.

The changes from before are drawn in boldface here.

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

public class ListeningExample extends JFrame implements ActionListener {
    JButton incr_button;
    JButton quit_button;
    JTextField number_field;

    public ListeningExample() {
        incr_button = new JButton("Increment");
        quit_button = new JButton("Quit");
        number_field = new JTextField();

        incr_button.addActionListener(this);
        quit_button.addActionListener(this);

        Container contents = getContentPane();
        contents.add(incr_button, BorderLayout.NORTH);
        contents.add(number_field, BorderLayout.CENTER);
        contents.add(quit_button, BorderLayout.SOUTH);
        pack();
    }

    public void actionPerformed(ActionEvent evt) {
        if(evt.getSource() == incr_button) {
            int i = Integer.parseInt(number_field.getText());
            number_field.setText("" + (i + 1));
        } else if(evt.getSource() == quit_button) {
            System.exit(0); // The System.exit class method halts entire program
        }
    }

    public static void main(String[] args) {
        (new ListeningExample()).show();
    }
}

The important changes here are that we've added two lines in the ListeningExample constructor method registering the ListeningExample object to ``listen'' to actions being performed on its two buttons. That is, when somebody clicks on either button, it will call the actionPerformed() method on the constructed ListeningExample object.

The other important change is the addition of the actionPerformed() method. What it does is first to see which component triggered the action - that is, whether the Increment or Quit button was clicked. It performs an action based on this.


Next: None. Up: Graphical user interfaces. Previous: Interfaces.