Java, set ID for JButton

user1321361 picture user1321361 · Apr 9, 2012 · Viewed 14.7k times · Source

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

Answer

Adam picture Adam · Apr 9, 2012

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
}