2

I'm developing a GUI in Java by use of NetBeans and I like to change the text color of a disabled button to black.

The following command is working fine with a combo box:

UIManager.getDefaults().put("ComboBox.disabledForeground", Color.BLACK);

With a Button the following commands have no effekt:

UIManager.getDefaults().put("Button.disabledForeground", Color.BLACK);

or

UIManager.getDefaults().put("Button.disabledText", Color.BLACK);

I hope somebody can help me.

Thank you in advance.
Steffen

jmj
  • 237,923
  • 42
  • 401
  • 438
Steffen Kühn
  • 21
  • 1
  • 2

3 Answers3

1
 UIManager.getDefaults().put("Button.disabledText",Color.RED);

working for me

jmj
  • 237,923
  • 42
  • 401
  • 438
  • That's strange, it is not working in my program. But for example "UIManager.getDefaults().put("Button.disabledShadow", Color.RED);" is working. But not with the text.... – Steffen Kühn Jan 28 '11 at 08:25
  • try with RED. probably in your system it is hard to make difference between gray and red – jmj Jan 28 '11 at 08:29
  • I tried different colors. However it is not working. I also created a simple test application to avoid side effekts. – Steffen Kühn Jan 28 '11 at 08:42
  • Whether or not this is honored, depends on the Look and Feel that is used. Not all L&F support this –  Jan 28 '11 at 08:47
  • OK, but for me that's a weird behaviour cos it's working fine for a combo box. Is there any workarround possible? – Steffen Kühn Jan 28 '11 at 09:03
0

it's working fine for a combo box.

UIManager Defaults will show what properties can be changed for your LAF.

Is there any workarround possible?

Not easily. This is part of the UI. So you would need to create and instal your own button UI. Sometimes it is relatively easy but most times you need access to protected or private methods.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

This question is quite old. However, I encountered the same problem and found no satisfactory answer.

So I ended up debugging the swing code. The color is determined by the getColor method in SynthStyle.class. This method uses the defaults for the disabled state only for JTextComponent and JLabel. It looks like the defaults "Button.disabledText" and "Button[Disabled].textForeground" are not used, at least with Nimbus LaF.

Finally, I extended JButton and override getForegroundColor:

public class PatchedButton extends JButton {

    public PatchedButton(){
        super();
    }

    public PatchedButton(Icon icon){
        super(icon);
    }

    public PatchedButton(String text){
        super(text);
    }

    public PatchedButton(String text, Icon icon){
        super(text, icon);
    }

    @Override
    public Color getForeground() {
        //workaround
        if (!isEnabled()) {
                return  (Color)UIManager.getLookAndFeelDefaults().get("Button.disabledText");
        }
        return super.getForeground();
    }
}
shizhen
  • 12,251
  • 9
  • 52
  • 88
Christian
  • 106
  • 5