Search code examples
javaswinglook-and-feeljfilechooseruimanager

Set filechooser Colors with Nimbus L&F


I'm using custom Colors with Nimbus. After hours of searching I can't find out how to set the Background and Foreground colors for JFileChooser properly.

My (non working) code:

UIManager.getLookAndFeelDefaults().put("FileChooser.background", Color.DARK_GRAY);  
UIManager.getLookAndFeelDefaults().put("FileChooser.textForeground", Color.white);  
UIManager.getLookAndFeelDefaults().put("FileChooser.foreground", Color.white);  
UIManager.getLookAndFeelDefaults().put("Label.foreground", Color.white);  

According to Oracle Nimbus defaults this should work, but doesn't. I also couldn't find the answer anywhere else.

What I want to change

I would like to have the Labels: (Look In:, Folder Name: Files of Type) displayed in white and the light gray borders displayed in dark gray.

Thanks in advance :)

Update: I could fix some Text colors with a detour:

UIManager.getLookAndFeelDefaults().put("textForeground", Color.white);
UIManager.getLookAndFeelDefaults().put("Menu.textForeground", Color.white);
UIManager.getLookAndFeelDefaults().put("ToolTip.textForeground", Color.BLACK);
UIManager.getLookAndFeelDefaults().put("List.textForeground", Color.BLACK);
UIManager.getLookAndFeelDefaults().put("TextField.foreground", Color.BLACK);
UIManager.getLookAndFeelDefaults().put("TextArea.foreground", Color.BLACK);
UIManager.getLookAndFeelDefaults().put("EditorPane.foreground", Color.BLACK);

However, the Frame Background of JFileChooser still remains Light Gray (while ALL other Frames/Dialogs and MessageDialogs honour the set backround color DarkGray).

Another weird one I now noticed as well is: The popupmenu respects the background color of JMenuItem but ignores the foreground. To illustrate what I mean I uploaded an a new IMAGE where I compare a "normal" popupmenu and one that appears inside JFileChooser.


Solution

  • I had identical problem, concerning changing background color of JFileChooser.

    My solution - new Painter. I guess it will be useful for your purpose too. Constants.APP_BACKGROUND_COLOR is a desired background color. And here is a code sample:

    UIManager.getLookAndFeelDefaults().put("FileChooser.background", Constants.APP_BACKGROUND_COLOR);
    
    UIManager.getLookAndFeelDefaults().put("FileChooser[Enabled].backgroundPainter",
                        new Painter<JFileChooser>()
                        {
                            @Override
                            public void paint(Graphics2D g, JFileChooser object, int width, int height)
                            {
                                g.setColor(Constants.APP_BACKGROUND_COLOR);
                                g.draw(object.getBounds());
    
                            }
                        });