Set another disabled color for two different JButtons? (UIManager.getDefaults changes both buttons)

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

Is there an easy way to do this?

UIManager.getDefaults().put("Button.disabledText",Color.BLUE); button1.setEnabled(false); UIManager.getDefaults().put("Button.disabledText",Color.RED); button2.setEnabled(false); 
+4
source share
3 answers

The appearance is determined by the ButtonUI specified in the user selected Look and Feel. If you create your own L & F, you can override getDisabledTextColor() . This related example may suggest how to proceed. Although technically possible, I'm not sure how users will interpret the difference.

Although this applies to your needs, the descendants of JTextComponent offer setDisabledTextColor() for this purpose.

+2
source

As thrashgod says, the ButtonUI button controls the disabled text color. Fortunately, you can change the ButtonUI button:

 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); 
+1
source
  /** * 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 is the code I found on

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

Hope this helps !!! I really did not understand the question: P

-1
source

Source: https://habr.com/ru/post/1379238/


All Articles