Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, set ID for JButton

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);
like image 345
user1321361 Avatar asked Apr 09 '12 07:04

user1321361


People also ask

How will you assign the string and icon both to the JButton?

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

How do you make a JButton Unclickable in Java?

We will use addActionListener(), ActionEvent() and setEnabled() on this button. Here we will call setEnabled(false) on the button. button. setEnabled(false);


1 Answers

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
}
like image 171
Adam Avatar answered Sep 27 '22 17:09

Adam