Windows look and feel for JFileChooser
Asked Answered
E

8

10

I'm trying to generate a JFileChooser that has the Windows look-and-feel. I couldn't find a method to change it, so I created a base class that extends JFileChooser that changes the UI with the following code:

public FileChooser(){
  this(null);
}
public FileChooser(String path){
   super(path);
   try {
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

    } catch (Exception e) { System.err.println("Error: " + e.getMessage()); }

Then, in another class, I call

FileChooser chooser = new FileChooser(fileName);
int val = chooser.showOpenDialog(null);

but the dialog box that comes up has the Java look and feel. Any thoughts on how to change this? Is there a method of the JFileChooser class that I can use instead of this extended class?

Thank you!

Entwine answered 17/2, 2010 at 16:3 Comment(2)
Do you want only the FileChooser to have the Windows L&F?Pinky
no - the whole application. I thought you had to change the UI for every component.Entwine
C
9

If you don't need to change the Look and Feel, could you try putting the UIManager.setLookAndFeel(..) line in the main method of your entry class?

That seems to work for me, though I am at a loss as to why it won't work the way you have set it upt.

Chobot answered 17/2, 2010 at 16:11 Comment(1)
The problem was that I set the look-and-feel after I displayed the JFileChooser. I also didn't understand that I could change the UI for the whole thing at once. Thank you for the hint.Entwine
W
15

I know you can set the look and feel for the whole application, but what do you do if you like the cross-platform look and feel but want the System Look and Feel for the JFileChoosers? Especially since the cross-platform doesn't even have the right file icons (and looks completely cheesy.)

Here is what I did. It is definitely a hack...

import sun.swing.FilePane;
import javax.swing.JFileChooser;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;

public class JSystemFileChooser extends JFileChooser{
    public void updateUI(){
        LookAndFeel old = UIManager.getLookAndFeel();
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Throwable ex) {
            old = null;
        }

        super.updateUI();

        if(old != null){
            FilePane filePane = findFilePane(this);
            filePane.setViewType(FilePane.VIEWTYPE_DETAILS);
            filePane.setViewType(FilePane.VIEWTYPE_LIST);

            Color background = UIManager.getColor("Label.background");
            setBackground(background);
            setOpaque(true);

            try {
                UIManager.setLookAndFeel(old);
            }
            catch (UnsupportedLookAndFeelException ignored) {} // shouldn't get here
        }
    }



    private static FilePane findFilePane(Container parent){
        for(Component comp: parent.getComponents()){
            if(FilePane.class.isInstance(comp)){
                return (FilePane)comp;
            }
            if(comp instanceof Container){
                Container cont = (Container)comp;
                if(cont.getComponentCount() > 0){
                    FilePane found = findFilePane(cont);
                    if (found != null) {
                        return found;
                    }
                }
            }
        }

        return null;
    }
}
Wight answered 3/12, 2010 at 21:51 Comment(2)
Huge thanks for providing copy-n-paste solution, you helped a lot :)Stigmatism
Awesome! Thanks!Afar
C
9

If you don't need to change the Look and Feel, could you try putting the UIManager.setLookAndFeel(..) line in the main method of your entry class?

That seems to work for me, though I am at a loss as to why it won't work the way you have set it upt.

Chobot answered 17/2, 2010 at 16:11 Comment(1)
The problem was that I set the look-and-feel after I displayed the JFileChooser. I also didn't understand that I could change the UI for the whole thing at once. Thank you for the hint.Entwine
S
4

First, try running the code from the command line and specify the look and feel there to see that it can be applied.

 java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel YourApp

If it does apply the correct look and feel then you can add the look and feel code to the program before you create the JFileChooser dialog. Lets say a simple program would look like this:

 public static void main(String[] args) {
try {

    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

} 
catch (Exception e) {
   // handle exception
}

JFileChooser chooser = new JFileChooser(); 
//etc
}
Stretcher answered 17/2, 2010 at 16:15 Comment(0)
U
4

The problem is that the Look & Feel was already selected for you when you called super(path).

From the Java Tutorial for Look and Feel:

Note: If you are going to set the L&F, you should do it as the very first step in your application. Otherwise you run the risk of initializing the Java L&F regardless of what L&F you've requested. This can happen inadvertently when a static field references a Swing class, which causes the L&F to be loaded. If no L&F has yet been specified, the default L&F for the JRE is loaded. For Sun's JRE the default is the Java L&F, for Apple's JRE the Apple L&F, and so forth.

To remedy, you should do this (explanation located here) - replace your try/catch block with this code:

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
this.pack();
Upbear answered 17/2, 2010 at 16:28 Comment(1)
this answer will probably help me, that's a good citation and succinct solutionShwalb
B
4

Try this at the beginning of your main method. Or if you are using netbeans or eclipse windowbuilder generated code, put this before the generated code.

try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } 
catch (UnsupportedLookAndFeelException e) {}
catch (ClassNotFoundException e) {}
catch (InstantiationException e) {}
catch (IllegalAccessException e) {}
Bibliotherapy answered 29/7, 2013 at 17:35 Comment(0)
C
1

Using the UIManager.setLookAndFeel(...); early in your main method should be the cleanest approach as explained previously but be very cautious about adding it to an existing application without extensive testing.

For instance, I tried changing the LAF to WindowsLookAndFeel (so that JFileChooser knows that "My Documents" actually refers to a folder named "Documents") and hit a NullPointerException in a different module due to the following line:

int separatorWidth = (new JToolBar.Separator()).getSeparatorSize().width;
Crag answered 20/1, 2016 at 1:59 Comment(1)
There's an answer with a method to override L&F for a single class in [9595358]: #9595858Crag
H
-1

To start off:

String path = null;
FileChooser fc=new FileChooser(path);
fc.showOpenDialog(null);

Then in another class:

public FileChooser(){
    this(null);
}

public  FileChooser(String path) {
    super(path);
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        SwingUtilities.updateComponentTreeUI(this);
        this.pack();
    } catch (Exception ex) {
        Logger.getLogger(FileChooser.class.getName()).log(Level.SEVERE, null, ex);
    }

    JFileChooser chooser = new JFileChooser();
}

private void pack() {
    try{
    }catch(UnsupportedOperationException eu){
    };
}
Hellespont answered 30/5, 2012 at 17:40 Comment(0)
I
-1

If you need this -> Windows Look And Feel Sample

Use can use the below code (too)!

Have fun!

JFrame w = new FileExplorerJFrame();

    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(ExcelTest.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        Logger.getLogger(ExcelTest.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        Logger.getLogger(ExcelTest.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnsupportedLookAndFeelException ex) {
        Logger.getLogger(ExcelTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    SwingUtilities.updateComponentTreeUI(w);

    w.pack();
    w.setVisible(true);
Interplanetary answered 21/11, 2015 at 2:29 Comment(1)
Which part? here the code is for FileExplorerJFrame(),Interplanetary

© 2022 - 2024 — McMap. All rights reserved.