views:

361

answers:

2

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?

+1  A: 

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.

akf
Just asking for selectedIcon, CheckBox.selectedIcon, or CheckBoxUI.selectedIcon, all return null.
Jay R.
You're answer inspired me to dig deeper in the Synth package to find out how the standard icon was converted. Thanks.
Jay R.
+2  A: 

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);
Jay R.