I'm currently learning java.swing and event handling and I've just been starting simple with one button and that changes the text of a label and that's it. The way I do this is I create an ActionListener and add it to the button component and then use the ActionPerformed method to change the text of the label. I'm now wondering about larger scale programs. Would you have to create a new ActionListener for every single component that the user can interact with? There must be a better way to do this, what is it?
-
No, you can create listeners to add to several components. It's not awfully common though, nor is it a big waste to create individual ones. – daniu Jul 31 '18 at 20:16
-
ok great so if i did this, it wouldn't be considered bad practice? – hyphxnated Jul 31 '18 at 20:19
-
Creating a new listener for every component is perfectly fine, and it's probably even preferable. See for example, [separation of concerns](https://en.wikipedia.org/wiki/Separation_of_concerns) and [single responsibility principle](https://en.wikipedia.org/wiki/Single_responsibility_principle). – Radiodef Jul 31 '18 at 20:46
4 Answers
Or you create once per listener that can be very succicint with lambdas expressions.
OR you keep the references of buttons and compare against action.getSource with an if/else statement, like:
Button x, y; //initialized somewhere
ActionListener l = new ActionListener() {
public void onActionPerformed(ActionEvent evt) {
if(evt.getSource() == x) {
//Do action for x
} else if (evt.getSource() == y) {
//Do action for y
}
}
x.addActionListener(l);
y.addActionListener(l);

- 18,136
- 30
- 106
- 167
-
-
2I really dislike the `if (evt.getSource() == x)... else if (evt.getSource() == y)` pattern. – daniu Jul 31 '18 at 20:35
-
The only problem is that if the action to execute (more than one line) inside the if statement are all inside the ActionListener, the best is to create separate methods for each and just call inside the if/else or use a Map – Marcos Vasconcelos Jul 31 '18 at 20:39
-
1If a button has different functionality, then create a separate listener. It is not a good design to have nested if/else statements. If a button has shared functionality than you can share the listener. See: https://stackoverflow.com/questions/33739623/how-to-add-a-shortcut-key-for-a-jbutton-in-java/33739732#33739732 for an example of this approach. – camickr Jul 31 '18 at 20:44
Well, since it is very unusual that two components do exactly the same, it is very likely that you need an own ActionListener for every component.
If, for whatever reason, two components do the same thing, then the easiest way I think of is simply outsourcing the code to a method and then call the method from the ActionListener

- 157
- 11
You could...
Make a specialised class which performs common operations which can be configured by passing parameters to the constructor
You could...
Make use of Anonymous Classes
You could...
Make use of the Actions API and create small re-usable operations.
This is similar to the first point, but provides a much more configurable unit of work which can easily be applied to menu items, buttons and key bindings
The ActionEvent
...
The ActionEvent
contains s number of use properties, the actionCommand
been probably one of the more common (at least until anonymous classes came along).
This allows you to decouple the source of the event from the action which should take place, allowing your to re-use ActionListener
s (or the same instance) on multiple objects.
Having said that, the Action
API has more or less made it redundant
Overall...
The intention would be to focus on what the core operations are and design re-usable and/or configurable classes which can be easily distilled or which can help reduce the overall repetition and duplication of common code.

- 1
- 1

- 343,457
- 22
- 230
- 366
I think there is no way to handle multiple buttons in the same action listener. but each time you clicked the button the whole textboxes can be handled in the same actionListener. In addition, it sounds not trivial to handle multiple buttons in the same actionListener

- 1
- 1
-
ok but it seems like it would be inefficient and really repetitive to create a new action listener for every single button you have throughout the entire program. or is it common practice? – hyphxnated Jul 31 '18 at 20:20
-
2The `ActionEvent` (to be more specific: the `EventObject`) has a `source` property which can be used to differentiate between buttons in the same listener, just FYI. – sn42 Jul 31 '18 at 20:23