aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Event Handling with Array of Buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Event Handling with Array of Buttons" Watch "Event Handling with Array of Buttons" New topic
Author

Event Handling with Array of Buttons

Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
I've got an array of java.awt.Buttons and I'm wondering what the best way to handle the event
processing.
Here's what I've got now:
public void actionPerformed(ActionEvent evt){
for (int i=1; i<10; i++ ){
if(evt.getSource()==myButtons[i]){
// call a method based on the array index
}}
This seems kinda bad for performance, but it was easier and faster than writing a bunch of if/else statements.
Is there a better way to do this?
Maybe there is a way to detect if any of the buttons in the array have been pressed?
Thanks,
Drew
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
don't use the "=="-operator with objects. use evt.getSource().equals(button[i]) instead.
you could attach an action command (String) to each button as an alternative.
chantal
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

If you were using Swing, you could use ButtonGroup. If you were using check boxes instead of buttons, you could use a CheckboxGroup. If you stick with using AWT could you use checkboxes instead of buttons?


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
I can't use Swing because it's not supported on the device and I can't use CheckBoxes because I'm using the buttons to increment a counter.
I don't really see that using "==" is the big bottleneck here.
I was more referring to a way to avoid the loop.
Drew
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
I forgot to mention that I've got 4 groups of 10 buttons. So, there are a lot of buttons in the loop that don't get fired, but I'm still checking them.
Drew
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
I didn't mean that "==" is the bottleneck. in case of comparing object references its simply wrong (or doesn't have the expected meaning).
chantal
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Well... it would help if you described what you were trying to do with all these buttons...

You could set the actionCommand on each button, add a separate ActionListener like the one below to it, and then put that ActionListener into a Hashtable using the actionCommand of the button as the key. Then, action handling is handled automatically by the listener, and when you need to see how many times a button has been pressed, you simply get your reference out of the Hashtable based on the actionCommand of the button you are concerned with.



Chantal is right about equals()... you shouldn't be comparing object references with ==... it doesn't mess up the code right now, since you are saving all of your object references... but if anything changes in your code and you aren't saving all your references, the == code won't work. It's just a bad practice to get into.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Event Handling with Array of Buttons