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);
To add icon to a button, use the Icon class, which will allow you to add an image to the button. Icon icon = new ImageIcon("E:\editicon. PNG"); JButton button7 = new JButton(icon);
We will use addActionListener(), ActionEvent() and setEnabled() on this button. Here we will call setEnabled(false) on the button. button. setEnabled(false);
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With