Search code examples
javaswingjbuttonlook-and-feelnimbus

Nimbus look-and-feel changes color of JButtons


I have a JButton with an icon on it. I want the background color of the JButton to be the same as the icon.

The following code works fine in the standard look-and-feel :

button.setBackground(new Color(212,208,199));

But when I change my look-and-feel to Nimbus, then the background color of the JButton is much lighter.

The JButton still changes its background color when I change the color in button.setBackground(), but I have no idea to what color I need in Nimbus to get the same color as the background color of the JButton. Of course I could try to find the color by sight by trying all values, but there should be a simpler way.

I also tried changing the background color via the following code, but with the same result :

UIDefaults defaults = UIManager.getLookAndFeelDefaults();
defaults.put("Button.background",new Color(212,208,199));

How can I change the background color of my JButton in Nimbus, so that it merges with the background color of the containing icon ?

Below are some pictures of the button with default LaF, nimbus LaF (same code), and nimbus LaF (red color) :

Default LaF, using button.setBackground(new Color(212,208,199)) :

enter image description here

Nimbus LaF, using button.setBackground(new Color(212,208,199)) :

enter image description here

Nimbus LaF, using button.setBackground(Color.red) :

enter image description here


Solution

  • The question still ponders me, as I would like to know why nimbus changes the outcome of setBackground() ... I assume nimbus applies some kind of color mask, which slightly changes the colors slightly?

    But : in my project the buttons are all in a separate JPanel, and the Jpanels which require the nimbus LaF don't have any buttons, so I solved the problem by using the default LaF for the buttons JPanel, and only use the nimbus LaF in the JPanels where I need it.

    Sorry, that I didn't think of this before (I somehow assumed the LaF had to apply to the whole project).

    So problem solved, but still curious to an answer about the nimbus color handling ....