3

I have a Swing JButton and I'm also using the following code for my project:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Now when trying to change the background for one button using btnNewButton.setBackground(Color.RED); it doesn't turn red, only the borders turn red.

How can I turn this background to red while still using UIManager.getSystemLookAndFeelClassName() for the rest of the components/project?

Omid
  • 823
  • 1
  • 11
  • 31
  • Does your button have a icon(image)? If so, the background does turn red but is hidden by the icon of the button, giving the impression that only the "borders" are changed. – Jonathan Drapeau Apr 10 '13 at 20:29
  • 1
    hmm, even without the icon it still happens so it's not that. As long as I have UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); only the borders change color... – Omid Apr 10 '13 at 20:32

3 Answers3

5

Take a look at bug 4880747 : XP L&F: REGRESSION: setBackground on JButton sets border color in Windows XP. Evaluation section states:

Changing the appearance of a button can always cause conflicts with the current L&F implementation. The Windows L&F for Swing tries to be as close as possible to the native display. On XP, we use the built-in bitmap resources for the buttons. These can not be colorized, just like in the native API.

You should call setContentAreaFilled(false) on the button to avoid having the L&F paint its decorations. This has the side effect that the button's opaque property is set to false, so you need to follow that call with a call to setOpaque(true).

This is not a bug and will be closed.

As stated, setContentAreaFilled(false) and setOpaque(true) will do the trick, but the button will look different.

If it worth the trouble you can create you own ButtonUI. Here is a great example by @mKorbel that you may find useful.

Community
  • 1
  • 1
tenorsax
  • 21,123
  • 9
  • 60
  • 107
1

This depends on what you want to achieve.

You could use JButton#setContentAreaFilled passing it false, but you, probably also need to call JButton#setBorderPainted passing it falls

You could also change the UIManager's default value for the buttons background

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • If we set `UIManager`'s default value then this change will be reflected for all `Jbuttons`..Isn't there any way to change the background of only one `JButton`? – Vishal K Apr 10 '13 at 21:08
  • Not really, while maintains the look of the button – MadProgrammer Apr 10 '13 at 21:24
  • +1 for `setContentAreaFilled`. It looks funny with `WindowsLookAndFeel` though :) – tenorsax Apr 10 '13 at 21:51
  • @VishalK I did think that you could provide your own UI delegate, but would mean you need to provide one for each possible look and feel you want to use, that's a lot of work – MadProgrammer Apr 10 '13 at 22:15
0

You should make the JButton opaque:

btnNewButton.setOpaque(true);

As specified for JComponent#setBackground method in oracle documentation:

Sets the background color of this component. The background color is used only if the component is opaque, and only by subclasses of JComponent or ComponentUI implementations. Direct subclasses of JComponent must override paintComponent to honor this property.

It is up to the look and feel to honor this property, some may choose to ignore it.

I think that the current look and feel does not support this property..That's why the background color is ignored in this case.

Vishal K
  • 12,976
  • 2
  • 27
  • 38