Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Event Handling with Array of Buttons

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic