System look and feel layout on JFileChooser but with nimbus look and feel theme
Asked Answered
V

2

9

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?

Vintager answered 19/8, 2011 at 2:6 Comment(4)
Are you asking if it's possible to change the look and feel of just one component?Heelpost
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.Vintager
Note that the Windows PLAF (layout etc.) is not available on *nix & Mac. (thankfully).Tecu
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.Vintager
C
7

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);
            }
        }
    }
}
Cappello answered 19/8, 2011 at 2:43 Comment(4)
+1, interesting hack. It also seems to work on JDK6 Metal LAF. Definitely a buyer beware solution.Whilom
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?Vintager
@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.Cappello
@Thanks again prunge. I'll play around with this later and see what I can do :)Vintager
W
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.

Whilom answered 19/8, 2011 at 2:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.