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
  • 232,312
  • 42
  • 391
  • 431
Steffen Kühn
  • 21
  • 1
  • 2

3 Answers3

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

working for me

jmj
  • 232,312
  • 42
  • 391
  • 431
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
  • 316,400
  • 19
  • 155
  • 279
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
  • 11,455
  • 9
  • 49
  • 81
Christian
  • 51
  • 2