Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing look and feel default icons?

I want to change the selected icon for a JCheckbox to a different icon, let's say for example the disabled selected icon for a JCheckbox. How can I get the disabled selected icon from the UIManager?

I tried UIManager.getIcon("CheckBoxUI.disabledSelectedIcon"); Is that the wrong icon property name or is that just the wrong way to get to that resource?

like image 343
Jay R. Avatar asked Nov 02 '09 21:11

Jay R.


People also ask

How to add icons to java?

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


2 Answers

Apparently there isn't one by default. At least, not when I'm trying to call it.

Just dumping the keys from UIManager.getLookAndFeelDefaults().keys() produces the following if the key contains CheckBox:

CheckBox.foreground
CheckBox.border
CheckBox.totalInsets
CheckBox.background
CheckBox.disabledText
CheckBox.margin
CheckBox.rollover
CheckBox.font
CheckBox.gradient
CheckBox.focus
CheckBox.icon
CheckBox.focusInputMap

After reading akf's answer, I started digging through the UIManager code in the plaf.synth packages and found calls that essentially delegate the null disableCheckedIcon to the look and feel classes to try to convert the standard .icon to a grayed version. So I ended up with this:

Icon checkedIcon = UIManager.getIcon("CheckBox.icon");
Icon dsiabledCheckedIcon = 
   UIManager.getLookAndFeel().
      getDisabledSelectedIcon(new JCheckBox(), checkedIcon);
like image 179
Jay R. Avatar answered Sep 23 '22 08:09

Jay R.


Looking through the code for AbstractButton, it appears that the disabledSelectedIcon is derived from the selectedIcon, unless it is specified on the AbstractButton (or JCheckBox in this case) through setDisabledSelectedIcon. With this being the case, calling UIManager.getIcon("...") will not return the object you are looking for.

EDIT:

Note that a JCheckBox has an icon field as defined in the AbstractButton API, just as a JButton can have an icon. It is an image that is displayed next to the text and is separate from the 'checked' or 'unchecked' box icon that you may be referring to.

The check/uncheck icon is handled by a single class, found with UIManager.getObject('CheckBox.icon'). It is a subclass Icon and handles both the painting of its checked and unchecked state. You can see examples of it in the various [L&F name]IconFactory classes.

like image 40
akf Avatar answered Sep 22 '22 08:09

akf