开发者

Windows look and feel for JFileChooser

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.showOp开发者_JAVA技巧enDialog(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!


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


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.


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


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
}


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


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;


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜