architecture: best way to implement a color palette

i’m building the gui for my app mostly using swing components. i know that at somepoint i am going to want to tweak the coloring of the interface. so instead of hardcoding the colors into each component, i’ve created a simple object to wrap public fields of type Color. however, this means that i have to create this aforementioned ColorPalette object to access it from within my gui components.

in action, it might look like this:


ColorPalette cp = new ColorPalette();
... (somewhere later in the component's code) ...
this.setBackground( cp.bkgd );

obviously this will save time later if i want to tweak colors, or even give the user control over some things. however, it seems kind of clunky. i had a suspicion that some java guru might have a slicker way to approach global setttings.

if i make the ColorPalette a final class, will that mean i don’t have to instantiate it each time?

any ideas approaches to this kind of a typical problem would be great.

thx.

It is possible to design your own look-and-feel. I have not done this, but I believe it would be the proper way to solve the problem in general. There’s some interesting stuff at http://java.sun.com/products/jfc/tsc/articles/lookandfeel_reference/index.html#system_colors which allows you to globally control the appearance of components. Or perhaps you can do something with the Substance look-and-feel which someone mentioned in another thread (Commander Keith?).

Look and feels usually use some kind of theme which can be overridden. Take a look at… lets see…

/demo/jfc/Metalworks/src/PropertiesMetalTheme.java

There you can see a property based theme, which supports the 3 primary/secondary colors and “black”/“white”.

You can also use UIManager.put for overriding specific things. Here is some list with the default values:
http://kaioa.com/k/JavaUIDefaults.txt

You use it like:
UIManager.put(“ScrollBar.background”, new ColorUIResource(255,255,255));

or:
UIManager.put(“ToolTip.border”, BorderFactory.createEmptyBorder());

hey great information. however, i’m definitely creating some custom UI elements such as a time ruler. however, it’s mostly made from components. i have attached the image of the interface. it’s purposely quite simple, and most of it is just various JComponents. you can see that they all share a simple color palette that i want to be able to change globally.

should i create a ColorPalette class to store my colors, then create it as a static field of a master abstract gui class? how would you design this?

any tips much appreciated.