ActionListener

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

Yes. The system does not know which events will be ignored, so it creates an Event object for each one.

ActionListener

A button listener must implement the ActionListener interface. ActionListener is an interface (not a class) that contains a single method:

public void actionPerformed( ActionEvent evt) ;

The ActionEvent parameter is an Event object that represents an event (a button click). It contains information that can be used in responding to the event. ActionListener is an interface. A class that implements the interface must contain an actionPerformed() method.

Import the package java.awt.event.* if you are dealing with events.

import java.awt.*; 
import java.awt.event.*;
import java.awt.*; 

class ButtonFrame extends JFrame implements ActionListener
{
  JButton bChange;

  ButtonFrame()     
  {
     . . . . . .
  }

  
  // listener method for the ActionListener interface
  public void actionPerformed( ActionEvent evt)
  {
     . . . . . .
  }

}

The listener object could be defined in a class separate from the frame class. In our example, the same class that holds the component is also the listener for its events.

QUESTION 10:

Our revised class ButtonFrame says that it implements ActionListener. What does this mean?