TLET
TLET

Reputation: 33

ActionListener different variable for each button

I have an array of JButtons, and I want to have each one change the value of their respective counterpart variables in another array, do I need to make a separate listener for each button?

Upvotes: 0

Views: 91

Answers (2)

Extreme Coders
Extreme Coders

Reputation: 3521

You do not need to make a separate listener for each buttons. One listener would do, But you have to query for the source using getSource() and accordingly decide your path of action.

If you do implement separate listeners then probably it might be easier to code but it may require extra memory. So it all depends on your requirements.

Upvotes: 1

FThompson
FThompson

Reputation: 28707

The cleanest approach is the make a separate listener for each button.

However, you could make one listener for all buttons which compares the action event's getSource() component with every JButton, and performs according to which one the source equals.

Upvotes: 1

Related Questions