Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add action listener that listens to multiple buttons

I'm trying to figure out what i am doing wrong with action listeners. I'm following multiple tutorials and yet netbeans and eclipse are giving me errors when im trying to use an action listener.

Below is a simple program that im trying to get a button working in.

What am i doing wrong?

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame;   public class calc extends JFrame implements ActionListener {        public static void main(String[] args) {          JFrame calcFrame = new JFrame();          calcFrame.setSize(100, 100);         calcFrame.setVisible(true);          JButton button1 = new JButton("1");         button1.addActionListener(this);          calcFrame.add(button1);     }      public void actionPerformed(ActionEvent e) {         if(e.getSource() == button1)     }    } 

the action listener is never registered because with the if(e.getSource() == button1) it cant see button1, errors saying cannot find symbol.

like image 539
user519670 Avatar asked May 09 '11 11:05

user519670


People also ask

Can a button have multiple action listeners?

This is particularly useful when you want to register more than one listeners in a single component, a button for instance. All you have to do to work with multiple listeners is: Create a class that extends JFrame and implements ActionListener . Create an number of these JFrames and put them in an array.

How do I add action listener to buttons?

We can do this by writing the code as below: public void actionPerformed(ActionEvent e) { numClicks++; text. setText("Button Clicked " + numClicks + " times"); Now, when the user clicks the Button b, the button fires an action event which invokes the action listener's actionPerformed method.

What is use of getSource () method?

The getSource method is used in the actionPerformed method to determine which button was clicked.


2 Answers

There is no this pointer in a static method. (I don't believe this code will even compile.)

You shouldn't be doing these things in a static method like main(); set things up in a constructor. I didn't compile or run this to see if it actually works, but give it a try.

public class Calc extends JFrame implements ActionListener {      private Button button1;      public Calc()     {         super();         this.setSize(100, 100);         this.setVisible(true);          this.button1 = new JButton("1");         this.button1.addActionListener(this);         this.add(button1);     }       public static void main(String[] args) {          Calc calc = new Calc();         calc.setVisible(true);     }      public void actionPerformed(ActionEvent e) {         if(e.getSource() == button1)     }    } 
like image 67
duffymo Avatar answered Sep 28 '22 16:09

duffymo


I'm amazed that nobody has mentioned using an action command. This is a pretty standard way of associating sources and listeners. Its really useful if;

  • you have multiple event sources that need to do the same thing (eg if you want the use to be able to press the enter key on a text field as an alternative to clicking a button next to it)
  • you don't have a ref to the component generating the event

see;

import java.awt.FlowLayout; import java.awt.event.ActionEvent;     import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane;  public class DontExtendJFrame implements ActionListener {    private enum Actions {     HELLO,     GOODBYE   }    public static void main(String[] args) {      DontExtendJFrame instance = new DontExtendJFrame();      JFrame frame = new JFrame("Test");     frame.setLayout(new FlowLayout());     frame.setSize(200, 100);      JButton hello = new JButton("Hello");     hello.setActionCommand(Actions.HELLO.name());     hello.addActionListener(instance);     frame.add(hello);      JButton goodbye = new JButton("Goodbye");     goodbye.setActionCommand(Actions.GOODBYE.name());     goodbye.addActionListener(instance);     frame.add(goodbye);      frame.setVisible(true);   }    @Override   public void actionPerformed(ActionEvent evt) {     if (evt.getActionCommand() == Actions.HELLO.name()) {       JOptionPane.showMessageDialog(null, "Hello");     } else if (evt.getActionCommand() == Actions.GOODBYE.name()) {       JOptionPane.showMessageDialog(null, "Goodbye");     }   } } 
like image 40
Qwerky Avatar answered Sep 28 '22 16:09

Qwerky