Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One action listener, two JButtons

I have two JButtons called "Left" and "Right". The "Left" button moves a rectangle object to the left and the "Right" button moves it to the right. I have one ActionListener in the class that acts as the listener for when either button is clicked. However I want different actions to happen when each are clicked. How can I distinguish, in the ActionListener, between which was clicked?

like image 263
CodyBugstein Avatar asked Jan 21 '13 16:01

CodyBugstein


People also ask

Can you have multiple action listeners Java?

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. This array could easily hold any class as long as it implements the ActionListener interface.

What is use of getSource () method?

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

What is action listener give one example?

You implement an action listener to define what should be done when an user performs certain operation. An action event occurs, whenever an action is performed by the user. Examples: When the user clicks a button, chooses a menu item, presses Enter in a text field.

What does ActionListener mean?

ActionListener is an interface (not a class) that contains a single method: public void actionPerformed( ActionEvent evt) ; A class that implements the interface must contain an actionPerformed() method. The ActionEvent parameter is an Event object that represents an event (a button click).


2 Answers

Set actionCommand to each of the button.

// Set the action commands to both the buttons.

 btnOne.setActionCommand("1");
 btnTwo.setActionCommand("2");

public void actionPerformed(ActionEvent e) {
 int action = Integer.parseInt(e.getActionCommand());

 switch(action) {
 case 1:
         //doSomething
         break;
 case 2: 
         // doSomething;
         break;
 }
}

UPDATE:

public class JBtnExample {
    public static void main(String[] args) {
        JButton btnOne = new JButton();
        JButton btnTwo = new JButton();

        ActionClass actionEvent = new ActionClass();

        btnOne.addActionListener(actionEvent);
                btnTwo.addActionListener(actionEvent);

        btnOne.setActionCommand("1");
        btnTwo.setActionCommand("2");
    }
} 

class ActionClass implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        int action = Integer.parseInt(e.getActionCommand());
        switch (action) {
        case 1:
            // DOSomething
            break;
        case 2:
            // DOSomething
            break;                          
        default:
            break;
        }
    }
}
like image 109
Amarnath Avatar answered Oct 03 '22 16:10

Amarnath


Quite easy with the getSource() method available to ActionEvent:

JButton leftButton, rightButton;

public void actionPerformed(ActionEvent e) {
  Object src = e.getSource();

  if (src == leftButton) {

  }
  else if (src == rightButton) {

  }
}
like image 31
Jack Avatar answered Oct 03 '22 17:10

Jack