Java, set ID for JButton

java, jbutton, swing

Solution

There is a property name which you could use:

newButton.setName(String.valueOf(objectCounter))

alternatively, you could use clientProperties which lets you store arbitrary values:

newButton.putClientProperty("id", Integer.valueOf(objectCounter))

To fetch the value from the client property map you'll need something like this.

Object property = newButton.getClientProperty("id");
if (property instanceof Integer) {
   int objectCounter = ((Integer)property);
   // do stuff
}

Problem

Is there anyway to set an id for a `JButton`. I'm used to it in Android. I'm looking for something like the following: ``` newButton.setId(objectcounter); ```

Original source