Java- Changing swing background colour?

25,076

Solution 1

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.

Solution 2

You might try these:

  • control
  • controlDkShadow
  • controlHighlight
  • controlLtHighlight
  • controlShadow

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

Solution 3

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

Solution 4

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...

Solution 5

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);
}
Share:
25,076
dalyons
Author by

dalyons

Updated on July 09, 2022

Comments

  • dalyons
    dalyons almost 2 years

    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.