4

I am trying to make one button have a disabled text of Red and the other disabled text of Blue, but when using the following code all it does is just make them both Red.

Is there a simple way of fixing this?

UIManager.getDefaults().put("Button.disabledText",Color.BLUE);
button1.setEnabled(false);
UIManager.getDefaults().put("Button.disabledText",Color.RED);
button2.setEnabled(false);
billyslp
  • 41
  • 1
  • 2

3 Answers3

2

As thrashgod says, the button's ButtonUI controls the disabled text color. Luckily, you can change a button's ButtonUI:

button1.setUI(new MetalButtonUI() {
    protected Color getDisabledTextColor() {
        return Color.BLUE;
    }
});
button1.setEnabled(false);
button2.setUI(new MetalButtonUI() {
    protected Color getDisabledTextColor() {
        return Color.RED;
    }
});
button2.setEnabled(false);
Mangara
  • 1,116
  • 1
  • 10
  • 23
2

The appearance is determined by the ButtonUI specified in the user's chosen Look & Feel. If you are creating your own L&F, you can override getDisabledTextColor(). This related example may suggest how to proceed. While it is technically possible, I'm not sure how users would interpret the difference.

Although it's tangential to your needs, descendants of JTextComponent offer setDisabledTextColor() for this purpose.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
-1
    /**
 * Sets the Color of the specified button.  Sets the button text Color to the inverse of the Color.
 *
 * @param button The button to set the Color of.
 * @param color The color to set the button to.
 */
private static void setButtonColor(JButton button, Color color)
{
    // Sets the button to the specified Color.
    button.setBackground(color);
    // Sets the button text to the inverse of the button Color.
    button.setForeground(new Color(color.getRGB() ^ Integer.MAX_VALUE));
}

Here's some code I found on

http://www.daniweb.com/software-development/java/threads/9791

Hope it helps!!! I really didn't understand the question :P

Griffin
  • 710
  • 2
  • 15
  • 29
  • 1
    I am trying to change the text while the button is disabled(button.setEnabled(false)). I believe the code you gave me will only affect the color when the buttons are enabled(button.setEnabled(true)). – billyslp Nov 02 '11 at 00:48