سؤال

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.

هل كانت مفيدة؟

المحلول

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

                        }
                    });

نصائح أخرى

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

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.

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top