Skip to main content

changing the default background color of Swing components?

9 replies [Last post]
bt755
Offline
Joined: 2006-02-17

Hello, does anyone know how to do this? Do I have to change a parameter in UIManager?

The default is the dark gray background, and I'd really like to change it to something else. Or even make it transparent by default. I know that I can setBackground() each element I create, but that is not a good solution.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
idk
Offline
Joined: 2005-01-12

> *.background=blue

One could use UIDefaults to do that.

[code]
UIDefaults uiDefaults = UIManager.getDefaults();
Color bgColor = new ColorUIResource(Color.BLUE);
for (Enumeration e = uiDefaults.keys(); e.hasMoreElements(); ) {
Object obj = e.nextElement();
if (obj instanceof String) {
if ( ((String)obj).matches(".*\\.background$")
&& uiDefaults.get(obj) instanceof Color ) {
uiDefaults.put(obj, bgColor);
}
}
}
[/code]

-Igor

shan-man
Offline
Joined: 2006-02-17

Hi,

While the previous suggestion of iterating through the UIDefaults would certainly work, it comes with performance implications. Some UI properties are lazily loaded by Swing since they may never be needed. By fetching each one of the UI properties, you're forcing them all to be fully loaded immediately.

Thanks!
Shannon

idk
Offline
Joined: 2005-01-12

> By fetching each one of the UI properties

The suggestion was to fetch value only if property name matches *.background pattern.
There are not that many of thouse.

Best,
-Igor

shan-man
Offline
Joined: 2006-02-17

Igor,

idk wrote:
> > By fetching each one of the UI properties
>
> The suggestion was to fetch value only if property
> name matches *.background pattern.
> There are not that many of thouse.

My apologies, I missed that! Since backgrounds are currently just colors, this won't force the loading of any lazy values like I previously mentioned.

So, while I'm already typing, I thought I'd mention another thought that came to mind. Note that we're talking in this thread only about replacing background colors. If you're using the Metal look and feel, note that that's all you'll get - replacing the background only replaces the background. The colors that are used to draw etched borders, etc., will remain the same. That is, you may get a RED button, but the raised border will be in greys. The approach I mentioned above using a custom MetalTheme will let you change all of these colors.

Thanks!
Shannon

shan-man
Offline
Joined: 2006-02-17

Hi!

bt755 wrote:
> Hello, does anyone know how to do this? Do I have to
> change a parameter in UIManager?
>
> The default is the dark gray background, and I'd
> really like to change it to something else. Or even
> make it transparent by default. I know that I can
> setBackground() each element I create, but that is
> not a good solution.

If you're targeting the Metal Look and Feel, one approach you could consider is creating and installing a custom MetalTheme. Overriding getPrimaryXColor() and/or getSecondaryXColor() in particular will allow you to change the colors used throughout the Metal Look and Feel.

Regards,
Shannon

augusto
Offline
Joined: 2003-06-11

Yeah, for Metal, I should have mentioned themes. Which would be a great thing to add (hint, hint) to the other look and feels, have a generic theme mechanism to apply to whatever the current look and feel is.

zander
Offline
Joined: 2003-06-13

> Yeah, for Metal, I should have mentioned themes.
> Which would be a great thing to add (hint, hint) to
> the other look and feels, have a generic theme
> mechanism to apply to whatever the current look and
> feel is.

This has been done in the L&F of UICompiler and it even provides a nice color-choser for all those colors;
if you have java-webstart; you can start this demo:
http://uic.sourceforge.net/demo/configdiag.jnlp

Look at http://uic.sourceforge.net/api/12/uic/themes/UICTheme.html for the way to install the L&F in your application.

If you feel this is worth a try; download and install uic.tgz from: http://sourceforge.net/project/showfiles.php?group_id=59493

Cheers!

augusto
Offline
Joined: 2003-06-11

Yes, unfortunately you have to set the values in the

UIManager explicitly. Something like this ...

ColorUIResource color = new Color(Color.BLUE);

UIManager.put("Button.background", color );
UIManager.put("EditorPane.background", color );
UIManager.put("ToggleButton.background", color );
UIManager.put("TabbedPane.background", color );
UIManager.put("CheckBoxMenuItem.background", color );
UIManager.put("FormattedTextField.background", color );
UIManager.put("Viewport.background", color );

and so on ...

I asked at JavaOne if there was a better way to set this, but there's really none. What would be really nice would be a resource file format, maybe like in Motif, that allows you to set these properties with wildcards, like

*.background=blue

The Synth look & feel xml files have ways of setting colors with wildcards on components, but then you have to build a whole look and feel from scratch just to change colors!

bt755
Offline
Joined: 2006-02-17

thanks augusto!