8

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);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1321361
  • 131
  • 1
  • 2
  • 8
  • 2
    *"I'm used to it in Android."* Things are probably done differently in Android than in J2SE. Get used to doing it the way the language usually does. When in Rome.. – Andrew Thompson Apr 09 '12 at 08:56

1 Answers1

16

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
}
Adam
  • 35,919
  • 9
  • 100
  • 137