Recall

Here is a template for you to write an application responses to user's click :
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MyClass extends OtherClass implements ActionListener {
    // your code
    method(...) {
       button.addActionListener(this); // this is an ActionListener
    }
    
    public void actionPerformed(ActionEvent e) {
       // response for the user click 
    }
}
What happens after you click the button ?
  1. button fires and ActionEvent e.
  2. button looks for the ActionListeners associate to it. In the example here, only this associates to it.
  3. executes the actionPerformed method, with the e created by step 1 passed to the method.

ActionEvent

Quesion:

What if I have several buttons, how can I create different ActionListeners?

Answer:

Yes. You can create as many as ActionListeners you like by
public ActionListener1 implements ActionListener {
   public void actionPerformed(ActionEvent e) {
      // your code
   }
}
and you can create ActionListener2, ActionListener3, ...
The problem is that, you have to pass information of the main class to all the ActionListeners. This may make the program more complicated.

Question

Can I still use just one ActionListener ?

Answer

Yes. But you have several buttons. They may response to user click in different way.

Question

So what can I do if I have several buttons but just want to use one ActionListener?

Answer

Buttons generate ActionEvent e when you click it. We can use the information from the ActionEvent e.

e.getSource()

e.getSource() get the source of the event (i.e. the button you click).

String e.getActionCommand()

e.getActionCommand() get this text on the button. Refer to the last handout.
e.getActionCommand() get the text on the button only if you didn't use button.setActionCommand(String s).
We can use button.setActionCommand(String s) to set the "action command" of the button. So the "action command" is no longer the text on the button.

ActionCommandTest.java

You can get the program here.
commandtest.gif
   1:import java.awt.*;
   2:import javax.swing.*;
   3:import java.awt.event.*;
   4:
   5:public class ActionCommandTest extends JFrame implements ActionListener {
   6:    private JLabel display = new JLabel();
   7:    private JPanel buttonPanel = new JPanel(new GridLayout(2,4));
   8:    public ActionCommandTest() {
   9:        getContentPane().add(display);
  10:        display.setHorizontalAlignment(JLabel.CENTER);
  11:        for(char ch = 'A'; ch < 'E'; ch++) {
  12:            JButton button = new JButton(String.valueOf(ch));
  13:            button.setActionCommand("char");
  14:            button.addActionListener(this);
  15:            buttonPanel.add(button);
  16:        }
  17:        
  18:        for(int i = 1; i < 5; i++) {
  19:            JButton button = new JButton(String.valueOf(i));
  20:            button.setActionCommand("int");
  21:            button.addActionListener(this);
  22:            buttonPanel.add(button);
  23:        }
  24:        
  25:        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
  26:    }
  27:
  28:    public void actionPerformed(ActionEvent e) {
  29:        if(e.getActionCommand().equals("int")) {
  30:            display.setText("You clicked on an integer button.");
  31:        } else if (e.getActionCommand().equals("char")) {
  32:            display.setText("You clicked on a character button.");
  33:        }
  34:    }
  35:    
  36:    public static void main(String[] args) {
  37:        JFrame f = new ActionCommandTest();
  38:        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39:        f.setSize(300,120);
  40:        f.setVisible(true);
  41:    }
  42:}

highlight of the program

ActionCommandTest2.java

You can get the program here.
   1:import java.awt.*;
   2:import javax.swing.*;
   3:import java.awt.event.*;
   4:
   5:public class ActionCommandTest2 extends JFrame  {
   6:    private JLabel display = new JLabel();
   7:    private JPanel buttonPanel = new JPanel(new GridLayout(2,4));
   8:    public ActionCommandTest2() {
   9:        getContentPane().add(display);
  10:        display.setHorizontalAlignment(JLabel.CENTER);
  11:        for(char ch = 'A'; ch < 'E'; ch++) {
  12:            JButton button = new JButton(String.valueOf(ch));
  13:            button.addActionListener(new CharListener(display));
  14:            buttonPanel.add(button);
  15:        }
  16:        
  17:        for(int i = 1; i < 5; i++) {
  18:            JButton button = new JButton(String.valueOf(i));
  19:            button.addActionListener(new IntListener(display));
  20:            buttonPanel.add(button);
  21:        }
  22:        
  23:        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
  24:    }
  25:
  26:    
  27:    public static void main(String[] args) {
  28:        JFrame f = new ActionCommandTest2();
  29:        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30:        f.setSize(300,120);
  31:        f.setVisible(true);
  32:    }
  33:}
  34:
  35:class CharListener implements ActionListener {
  36:    private JLabel label;
  37:    public CharListener(JLabel label) {
  38:        this.label = label;
  39:    }
  40:    public void actionPerformed(ActionEvent e) {
  41:           label.setText("You clicked on a character button.");
  42:    }
  43:}
  44:
  45:class IntListener implements ActionListener {
  46:    private JLabel label;
  47:    public IntListener(JLabel label) {
  48:        this.label = label;
  49:    }
  50:    public void actionPerformed(ActionEvent e) {
  51:        label.setText("You clicked on an integer button.");
  52:    }
  53:}

Highlight