8

The windows look and feel layout on JFileChooser is much better than other look and feels like nimbus.

So I'm looking for a way to have the layout of a system look and feel but have nimbus or others theme on top.

Is this possible? If so how can it be done?

mKorbel
  • 108,320
  • 17
  • 126
  • 296
Daniel Ryan
  • 6,566
  • 4
  • 40
  • 60

2 Answers2

6

It's possible, though I don't know whether it's recommended. I managed to get it to work by asking the view to update itself on all but the topmost JFileChooser component (since that would replace all the chooser components with the Nimbus ones which you don't want).

I'd regard this as a hack that may or may not work depending on the internals of the Windows look and feel. It relies on pretty much the whole JFileChooser being built up by Swing components. If it ever was changed to use more direct native rendering (i.e. Java asks Windows to paint a significant portion of the chooser), it wont work. Don't know how well that trick will work with other components.

Anyway, this code seemed to work with JDK 7:

package test;

import java.awt.Component;

import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.plaf.nimbus.NimbusLookAndFeel; //Or use com.sun.... if you are using JDK < 7

public class LAFTester
{
    public static void main(String... args)
    throws Exception
    {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        JFileChooser chooser = new JFileChooser();
        chooser.updateUI(); //Create UI objects
        UIManager.setLookAndFeel(NimbusLookAndFeel.class.getName()); //Now set look and feel
        //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); //works with metal as well
        refreshUI(chooser, false);

        chooser.showOpenDialog(null);
    }

    private static void refreshUI(JComponent c, boolean includeParent)
    {
        if (includeParent)
            c.updateUI();

        for (int i = 0; i < c.getComponentCount(); i++)
        {
            Component child = c.getComponent(i);
            if (child instanceof JComponent)
            {
                refreshUI((JComponent)child, true);
            }
        }
    }
}
prunge
  • 20,579
  • 3
  • 71
  • 75
  • 1
    +1, interesting hack. It also seems to work on JDK6 Metal LAF. Definitely a buyer beware solution. – camickr Aug 19 '11 at 02:55
  • Awesome stuff that does work. However the colouring isn't perfect. So maybe I'll just target the title bar for now. Do you know how would I just target the title bar? – Daniel Ryan Aug 19 '11 at 03:01
  • 1
    @Zammbi you could potentially target individual components by not refreshing the UI on every component but only specific component(s), possibly by doing instanceof checks in refreshUI() - some of the components that make up the Windows JFileChooser UI are in specialized classes. – prunge Aug 19 '11 at 04:17
  • @Thanks again prunge. I'll play around with this later and see what I can do :) – Daniel Ryan Aug 19 '11 at 04:20
2

I assume you are talking about the panel on the left side of the Windows file chooser dialog which has Desktop, My Computer My Documents icons?

Well, I doubt this can be done because this is LAF specific. This was added to the Windows LAF because that is what the Windows platform file choose looks like. It is not support in other LAF's.

camickr
  • 308,339
  • 18
  • 152
  • 272