I have for example 10 buttons in my frame. Can i use a unique action listener for all of them? i mean not to define listener for each of them but one listener for all. please give an example. thanks
-
1It's possible ... but you'd need to use a `switch` block or a bunch of `if-then-else` blocks in order to execute the code that was appropriate to the button. This solution is fragile, difficult to maintain, and difficult to read. Moreover, it offers no performance benefit. There's no good reason to use this pattern. A single method or single Function Object for each button is a better idea. One possible exception would be if all the buttons executed the same code, but even then it would be better to implement the shared code in a private helper method. – scottb Aug 06 '13 at 21:11
-
"yes" to the first question, "go try and come back with any problems" for the second. – Richard Sitze Aug 07 '13 at 03:38
5 Answers
You could do it, but then you would have to de-multiplex all of those calls going to the "one controller" that rules them all, unless you have the unique circumstance where all of the buttons should do the same thing.
That de-multiplexing would probably then involve a switch (or worse, a bunch of if / then statements), and that switch would become a maintenance headache. See examples of how this could be bad here.

- 69,361
- 7
- 100
- 138
// Create your buttons.
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
// ...
// Create the action listener to add to your buttons.
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Execute whatever should happen on button click.
}
}
// Add the action listener to your buttons.
button1.addActionListener(actionListener);
button2.addActionListener(actionListener);
button3.addActionListener(actionListener);
// ...

- 25,722
- 2
- 45
- 57
yes, of course you can
class myActionListener implements ActionListener {
...
public void actionPerformed(ActionEvent e) {
// your handle button click code
}
}
and in the JFrame
myActionListener listener = new myActionListener ();
button1.addActionListener(listener );
button2.addActionListener(listener );
button3.addActionListener(listener );
and so on

- 11,411
- 10
- 42
- 70
-
Note: You can use the `if(e.getSource() == button1)` or `if(e.getActionCommand().equals("Button 1"))` (if the button was given any text, like `new JButton("Button 1");`) to determine which button was pressed. – Brendan Jun 26 '19 at 17:43
Check the source
on the ActionEvent
, and do an equality check against each of your buttons to see which caused the ActionEvent
. Then you can use your single listener for all buttons.
final JButton button1 = new JButton();
final JButton button2 = new JButton();
ActionListener l = new ActionListener() {
public void actionPerformed(final ActionEvent e) {
if (e.getSource() == button1) {
// do button1 action
} else if (e.getSource() == button2) {
// do button2 action
}
}
};
button1.addActionListener(l);
button2.addActionListener(l);

- 10,559
- 3
- 54
- 60
You could do this, In the actionPerformed
method get the action command and for each button set the action command
ActionListener al_button1 = new ActionListner(){
@Override
public void actionPerformed(ActionEvent e){
String action = e.getActionCommand();
if (action.equals("button1Command")){
//do button 1 command
} else if (action.equals("button2Command")){
//do button 2 command
}
//...
}
}
//add listener to the buttons
button1.addActionListener(al_buttons)
button2.addActionListener(al_buttons)
// set actioncommand for buttons
button1.setActionCommand("button1Command");
button2.setActionCommand("button2Command");

- 10,629
- 7
- 33
- 48