Accessing look and feel default icons?
Asked Answered
A

2

9

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?

Angloamerican answered 2/11, 2009 at 21:10 Comment(0)
A
9

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);
Angloamerican answered 2/11, 2009 at 22:24 Comment(0)
S
3

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.

Sinew answered 2/11, 2009 at 21:47 Comment(2)
Just asking for selectedIcon, CheckBox.selectedIcon, or CheckBoxUI.selectedIcon, all return null.Angloamerican
You're answer inspired me to dig deeper in the Synth package to find out how the standard icon was converted. Thanks.Angloamerican

© 2022 - 2024 — McMap. All rights reserved.