wood burning stoves 2.0*
The moose likes Java in General and the fly likes array of Jbutton - how to use event.getSource() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "array of Jbutton - how to use event.getSource()" Watch "array of Jbutton - how to use event.getSource()" New topic
Author

array of Jbutton - how to use event.getSource()

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
So....

Assuming I have an array of buttons (30) how can I tell which one is pressed when it gets to the actionPerformed area.

say I have this class:

Class ABC implements ActionListener
{

private JButton myButton[] = new JButton ("na");

public ABC()
{
//initilizing

}
.
.
.
if (event.getSource()==myButton[i])
{
//Question - what is i ???
// do somthing here
}

do i have to write 30 if (event.getSource()

thanks for any help
Dror Guzman
Greenhorn

Joined: Mar 07, 2005
Posts: 5
Do this:

public void actionPerformed(ActionEvent e){

for(int i=0; i<MyButton.length; i++){
if(e.getSource() == MyButton[i])
// do your event here.....
}
}
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
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..
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array of Jbutton - how to use event.getSource()
 
Similar Threads
Problem with "abstract class
Can I distinguish JButtons?
Need help with addActionListener
Event handling for dynamically created components
Buttons Shape.