0

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.

JavaTechnical
  • 8,846
  • 8
  • 61
  • 97
TomDK
  • 1,321
  • 11
  • 16

4 Answers4

4

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());

                        }
                    });
  • Thanks for you answer Nikola. I assume your code works. I have found a solution that fixes my problem - by settings the nimbusBase color. I have selected your answer as correct as it specifically sets the color for JFilechooser which I have asked in my question originally. – TomDK Jul 15 '13 at 09:55
1

You might want to check out http://docs.oracle.com/javase/7/docs/api/javax/swing/plaf/nimbus/NimbusStyle.html for some useful information on overriding the Nimbus color scheme.

Using the code below, I have managed to change the Nimbus color scheme for a jProgesssBar before. I've adapted it a bit so that it might work for your jFileChooser. Hope this works!

    UIDefaults defaults = new UIDefaults();
    defaults.put("FileChooser.background", Color.DARK_GRAY);
    defaults.put("FileChooser.textForeground", Color.white);

    yourJFileChooser.putClientProperty("Nimbus.Overrides.InheritDefaults", Boolean.TRUE);
    yourJFileChooser.putClientProperty("Nimbus.Overrides", defaults);
Frank D.
  • 1,306
  • 1
  • 13
  • 23
  • Thank you for your quick response but sadly that didn't do anything :( Also using: [code]defaults.put("FileChooser.textForeground", Color.white);[/code] I don't know why this one Component keeps playing up so much. However i could get it to a usable state by mounting this horse backwards. I set the default text color default textForeground as White and fixed set it black for components that need a Black font (e.g. List) – TomDK May 03 '13 at 17:13
0

The Nimbus Look And Feel UI defaults has a lot of bugs that are yet to be rectified. Some work, like the JProgressBar and JButton background gradient and some don't. Even i have tried setting the same thing. It is better if you write your own code instead of using these defaults or just wait for an update that fixes these bugs.

Just get the components of JFileChooser using the getComponents() method and then do the thing.

JavaTechnical
  • 8,846
  • 8
  • 61
  • 97
  • yes, not, not and (or just wait for an update that fixes these bugs.) wait untill we are died .... Swing is in maintanace mode, no changes up to Java8 – mKorbel Jul 13 '13 at 21:06
  • Thanks for your answer. Nimbus is partially ridiculous when you want to set the Background colors. For JTabbedPane tabs you need to rewrite the Paint Method. By setting the base color, however I got it working and it look beautiful. – TomDK Jul 15 '13 at 09:52
  • Waiting for Java 8, Let the Nimbus be the king! – JavaTechnical Jul 15 '13 at 09:55
0

Thanks everybody for your answer and apologies for my late own reply as I have been busy working on other components.

I wanted a dark Theme for my application that was consistent across the entire app. My solution that worked for me was simply:

Set the DEFAULT color for Nimbus.

UIManager.put("nimbusBase", Color.DARK_GRAY);

This has done the Trick with the Menus inside JFilechooser. It also fixes problems with JTabbed Pane etc.

All I needed to do now was fix up texts as they wery black on Dark Gray.

TomDK
  • 1,321
  • 11
  • 16
  • I have selected Nikola Dragićević's answer as correct as I haven't asked my question properly. – TomDK Jul 15 '13 at 09:49