2

I am writing a Java program in which I have an array of buttons (not a calculator!) and I'm looking for an efficient way to know which JButton was clicked. From what I know of Java so far, the only way to do it is to have them all in the same ActionListener and then loop through looking for a match.

Another solution I just thought of would be to extend JButton to include a unique ID number variable in the constructor. It seems that should work when the event object is cast to JButton after an instanceof check. Kind of like using VB's Tag property which is assigned to the index number.

Is there a better/more elegant way?

6
  • You can assign a unique ID to each JButton by using the setName and getName methods. Commented Jul 2, 2015 at 19:38
  • Careful with the setName it might get nulled if set before the action. Commented Jul 2, 2015 at 19:40
  • That's a possibility. I didn't know of those methods and they are part of JComponent. That's a good thing to know. That will actually help in another program also. Thanks! Commented Jul 2, 2015 at 19:44
  • Dima, do you mean overwritten somehow? Commented Jul 2, 2015 at 19:45
  • 1
    If you create a JButton then call setName("name") on it and after that call setAction(new AbstractAction(...)) the next call to getName will return null. Have a look at this Commented Jul 2, 2015 at 19:47

4 Answers 4

5

Is there a better/more elegant way?

  • yes to use (for almost JComponents) the put/getClientProperty, there you can to set endless number of properties, can be multiplied in contrast with setName / setActionCommand / etc

  • getClientProperty can be used as unique identificator for Swing Action or EventHandler (rather than to use ActionListener)

Links to the Javadocs: putClientProperty(), getClientProperty()

Sign up to request clarification or add additional context in comments.

3 Comments

Wasn't aware of these methods, very nice.
@Sean Bright please which one 1:-). ClientProperty or (my favorite) 2:-). EventHandler
put/getClientProperty
0

Here is an example from programm I writing last few months. I have an enum called Position

public enum Position {
    BB, SB, BU, CO, MP3, MP2, MP1, EP3, EP2, EP1;
}

and have some JToggleButtons each of them holding ist own Position.

public class PositionButton extends JToggleButton {
    private final Position p;

    public PositionButton(Position p) {
        this.p = p;
        setText(p.toString());
        setActionCommand(p.toString());
    }

    public Position getPosition() {
        return p;
    }
}

This allows you to create Buttons in a Loop and get value direct from button without comparing:

ActionListener positionListener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        PositionButton b = (PositionButton )e.getSource();
        Position p = b.getPosition();
        //do something with Position
    }
}

for (Position p : Position.values()) {
    PositionButton b = new PositionButton (p);
    b.addActionListener(positionListener);
}

Comments

0

The way I've done it before was using actionPerformed for different buttons. I like it more compared to some other ways I've seen.

public void actionPerformed(ActionEvent clicking)
{
    if (clicking.getSource() == button[0])
        // Do this
    if (clicking.getSource() == button[1])
        // Do something different
}

Since you built an array, you can throw the ID right where that 0 is and that's your unique ID.

Comments

-1

Add a separate action listener for each button.

1 Comment

The array I'll be using has 64 JButtons. Would there be any overhead or slowdown to have that many ActionListeners? If not for that many, would some number more? That's just for future reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.