9
votes

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?

2
Are you asking if it's possible to change the look and feel of just one component?mre
No I already know how to do that. I want to get a layout of one look and feel and use a theme of another.Daniel Ryan
Note that the Windows PLAF (layout etc.) is not available on *nix & Mac. (thankfully).Andrew Thompson
Yeah I know hehe. Though the JFileChooser system look and feel layout is worse on Mac than Windows. Though may use Quaqua library to solve that one.Daniel Ryan

2 Answers

7
votes

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);
            }
        }
    }
}
2
votes

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.