Windows look and feel for JFileChooser

32,924

Solution 1

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.

Solution 2

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...

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

Solution 3

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) {}

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

Solution 5

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
}
Share:
32,924
chama
Author by

chama

Updated on July 15, 2022

Comments

  • chama
    chama almost 2 years

    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!

  • chama
    chama over 14 years
    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.
  • Yura Taras
    Yura Taras about 12 years
    Huge thanks for providing copy-n-paste solution, you helped a lot :)
  • memnoch_proxy
    memnoch_proxy over 10 years
    this answer will probably help me, that's a good citation and succinct solution
  • RobB
    RobB over 8 years
    There's an answer with a method to override L&F for a single class in [9595358]: stackoverflow.com/questions/9595358/…
  • Mahsa
    Mahsa over 6 years
    Awesome! Thanks!
  • VirtualBlade
    VirtualBlade over 5 years
    Which part? here the code is for FileExplorerJFrame(),