Swing Listeners

A Swing component generates an event when

  1. The user performs some action with the mouse on the component, or
  2. The component has the focus and the user types at the keyboard.

To respond to these user events, you need to add a listener to the component. A listener is added to a component by sending an addXXXListener() message to the component, where XXX is the type of the event. To know what type of listener is appropriate, you should see the components documentation.

When an XXXEvent is generated, a method of the XXXListener class will be invoked. The name of the method depends on the type of listener. It can be determined by looking at the appropriate class description in the following sections.

A common construction for adding listener uses anonymous constructors. For example, an action listener can be added to a button with the following code.

    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        statements to implement response to button press
      }
    });
      

Action Listener

An ActionEvent is generated by a variety of user actions such as clicking a button, hitting the return key in a text field, or selecting a menu item. Programming a response to an ActionEvent involves writing an implementation of the ActionListener interface, which requires implementing an actionPerformed() method.

The ActionEvent parameter is often ignored.

Action Interface

The Action interface extends the ActionListener interface, adding methods for storing and retrieving additional information about an action such as a label or an icon for buttons, or a short description for tool tip text. Many of the components that use action listeners have constructors with an Action parameter. This makes it possible to define responses to the user that are enabled in several different ways. Programs often have an extensive set of menus with the most frequently used menu item also available as tool bar buttons. This is accomplished by creating a single Action and using it to construct both the tool bar button and the menu item.

The Action interface also defines String constants that should be used for the String parameter of the above methods.

Although the Action interface can be used as a variable type, Action objects are usually constructed with an an anonymous constructor that subclasses the AbstractAction class described below.

Abstract Action

The AbstractAction class implements all of the Action interface methods except for the actionPerformed() method.

The following template can be used for defining Action variables.

    Action action = new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
	    statements to implement action response
      }
    });
    // send putValue() messages to action to configure it.
      

Change Listener

A ChangeEvent is generated when the user adjusts a JSlider or when a JCheckBox or JToggleButton changes state. You can program a response to the ChangeEvent by creating a ChangeListener and registering it with the JSlider, JCheckBox, or JToggleButton. The response is implemented in the ChangeListener's stateChanged() method.

ItemListener

An ItemEvent is generated when the user selects one of the items in a JComboBox. You can program a response to the ItemEvent by creating a ItemListener and registering it with the JComboBox. The response is implemented in the ItemListener's itemStateChanged() method.

List Selection Listener

A ListSelectionEvent is generated by when the user selects one of the items in a JList. You can program a response to the ListSelectionEvent by creating a ListSelectionListener and registering it with the JList. The response is implemented in the ListSelectionListener's valueChanged() method.