Dror is correct assuming that your array is globally available.
I have run into a similar question as this topic numerous times dealing with either arrays which are not globally accessable or the JButton (or whatever) were not saved at all as the screen was dynamically created. In this case I would use JButton.setActionCommand(String) where the String I would give it would be which button it is... ie: Button 1, Button 2, etc..
I typically prefer to have one ActionListener per button.
How do the buttons get created, and what should happen if you press them?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I like one action listener per button. I also like an alternative with one action listener per window and a command per button.
This could just as easily be a listener per button since the code is so darned short. I tend to use "collection of commands to replace if-else-if" a lot. It might be the "when your only tool is a hammer everything looks like a nail" syndrome.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Jul 11, 2001
The Command solution looks nice if you want to decouple the creation of the buttons from the definition of what the buttons actually do (for example if you build some kind of reusable gui).
Otherwise it just seems to be more complex and opens the door for bugs that can't happen with the one-listener-per-button approach (such as not finding a command for a button), without any benefits?