dalyons
dalyons

Reputation: 1304

Java- Changing swing background colour?

Ok so ive got a swing app going using the "System" look and feel. Now, I want to change the background colour of the main panels to black. Too easy right?

UIManager.put("Panel.background", Color.BLACK);

Well yeah, except now the controls in the app look stupid, because their 'shadows', for want of a better word, are graduated to fade towards the old system default colour(gross windows grey). So there are light grey 'corners' on all the controls, especially the tabs on JTabbedPane. I know it can be fixed, because if you change the windowsXP theme to one with a different default application colour, the controls take on this changed colour and their shadows 'fade' towards it.

But I have no idea what UIManager key it is, or even if you can do it with UIManger.

I dont really want to change the L&F engine, because apart from this it looks good.

Upvotes: 2

Views: 25285

Answers (5)

Rastislav Komara
Rastislav Komara

Reputation: 1721

In general this is a little bit tricky. It depends on exact LaF you are using.

For example. JGoodies use own color scheme which redefine this stuff.

In general the property names are composed like

COMPONENT_NAME_WITHOUT_J + '.' + PROPERTY. 

Unfortunately, property names can be only obtained from implementation classes of LaF. These aren't shared or something. Each component has its own. Or better, it depends on laziness of author which pairs he used. In general.

A lot of help makes redefine Panel.* and Button.. A lot of components use Button. properties.

Try, play, win :). I wish you luck :).

PS: It is a lot of properties to overwrite. But this is the way how LaFs works.

Upvotes: 2

gtiwari333
gtiwari333

Reputation: 25156

To list out all the possible options that we can set to UIManager to change LaF, run the code below ........

 import java.util.*;
  import javax.swing.UIManager;

  public class UIManager_All_Put_Options
  {
    public static void main (String[] args)
    {
      Hashtable   properties = UIManager.getDefaults();
      Enumeration keys       = properties.keys();

      while (keys.hasMoreElements()) {
        String key   = (String) keys.nextElement();
        Object value = properties.get (key);
        System.out.printf("%-40s \t %-200s \n", key,value);
      }
    }
  }

enjoy...

Upvotes: 1

Craigo
Craigo

Reputation: 3727

Some controls like JButton need to have setOpaque(false) called to allow the new background colors to fade through.

Upvotes: 1

RodeoClown
RodeoClown

Reputation: 13798

You might try these:

  • control
  • controlDkShadow
  • controlHighlight
  • controlLtHighlight
  • controlShadow

(I just found them in this list: Swing [Archive] - UIManager: setting background and JScrollBar )

Upvotes: 2

RodeoClown
RodeoClown

Reputation: 13798

You can see what the default settings (and their keys) are by using UIManager.getDefaults(); You can then iterate over the resulting keySet (it is an instance of Map).

So something like this will show all the default keys:

for (Object key: UIManager.getDefaults().keySet())
{
    System.out.println(key);
}

Upvotes: 1

Related Questions