views:

7184

answers:

8

Ok so ive got a bitchin 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.

Help?

A: 

jjnguy, tried that already. Dosent help in the slightest! :P

These are like, shadows around controls, not their default background stuff which is painted inside them.

dalyons
Sorry...I'm stumped then.
jjnguy
+1  A: 

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);
}
RodeoClown
A: 

RodeoClown, I have already done that too! I perhaps should have said so originally.

Problem there is a) the list is massive, b) ive changed all the ones that look remotly like what I am after, eg:

UIManager.put("TabbedPane.shadow", Color.BLACK);
UIManager.put("TabbedPane.background", Color.BLACK);

and most of them do nothing :/

dalyons
+1  A: 

You might try these:

  • control
  • controlDkShadow
  • controlHighlight
  • controlLtHighlight
  • controlShadow

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

RodeoClown
A: 

thanks again, I tried those ones you suggested...

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

UIManager.put("controlDkShadow",  Color.GREEN);
UIManager.put("controlHighlight",  Color.GREEN);
UIManager.put("controlLtHighlight",  Color.GREEN);
UIManager.put("controlShadow",  Color.GREEN);

SwingUtilities.updateComponentTreeUI(this.getFrame());

Gives me black backgrounds to my panels, but green is nowhere to be seen!

Argh!

dalyons
That was just a guess from the list - trial and error is looking like your best option to find the right properties :SSorry I couldn't be of more help.
RodeoClown
A: 

Tried using ColorUIResource.GREEN instead of Color.Green.

...still no good.

Although for some reason the app flashes a green background before drawing the tabs.

Sample code for great justice:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;

/**
 *
 * @author Administrator
 */
public class Main extends JFrame {

    public static void main(String args[]) {
            try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
            catch (Exception e) {}

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

            UIManager.put("control",  ColorUIResource.GREEN);
            UIManager.put("controlDkShadow",  ColorUIResource.GREEN);
            UIManager.put("controlHighlight",  ColorUIResource.GREEN);
            UIManager.put("controlLtHighlight",  ColorUIResource.GREEN);
            UIManager.put("controlShadow",  ColorUIResource.GREEN);
            UIManager.put("TabbedPane.tabAreaBackground",  ColorUIResource.GREEN);  
            UIManager.put("TabbedPane.darkShadow",  ColorUIResource.GREEN); 
            UIManager.put("TabbedPane.borderHightlightColor",  ColorUIResource.GREEN); 
            UIManager.put("TabbedPane.unselectedTabHighlight",  ColorUIResource.GREEN); 
            UIManager.put("TabbedPane.unselectedTabShadow",  ColorUIResource.GREEN); 
            UIManager.put("TabbedPane.shadow",  ColorUIResource.GREEN); 

        new Main();
    }
    Main() {

     JLabel jlbHelloWorld = new JLabel("Hello World");
                JTabbedPane main_tabs = new JTabbedPane();
                main_tabs.setName("main_tabs");
                JTextArea ta1 = new JTextArea();
                JTextArea ta2 = new JTextArea();
                main_tabs.addTab("tab1", ta1);
                main_tabs.addTab("tab2", ta2);

     add(jlbHelloWorld);
                add(main_tabs);
     this.setSize(700, 700);
     setVisible(true);
    }
}
dalyons
+2  A: 

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. This properties aren't shared or something like this. 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.

Rastislav Komara
A: 

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

Craigo