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?

Tom K
  • 321
  • 1
  • 4
  • 15
  • You can assign a unique ID to each JButton by using the setName and getName methods. – Gilbert Le Blanc Jul 02 '15 at 19:38
  • Careful with the `setName` it might get `null`ed if set before the action. – Dima Maligin Jul 02 '15 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! – Tom K Jul 02 '15 at 19:44
  • Dima, do you mean overwritten somehow? – Tom K Jul 02 '15 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](http://stackoverflow.com/questions/30851673/java-jbutton-setaction-nulls-button-text) – Dima Maligin Jul 02 '15 at 19:47
  • Oops, I said JComponent. getName/setName are from Component. – Tom K Jul 02 '15 at 19:51

4 Answers4

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()

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
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.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
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);
}
andronix
  • 124
  • 9
-1

Add a separate action listener for each button.

FredK
  • 4,094
  • 1
  • 9
  • 11
  • 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. – Tom K Jul 02 '15 at 19:40