JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));
    chooser.setFileFilter(new javax.swing.filechooser.FileFilter()
     {
      public boolean accept(File f)
        {
         return f.getName().toLowerCase().endsWith(".txt");
         }
       public String getDescription()
        {
         return "文本文档(*.txt)";
        
        }
     }
    );我用上面的语句来在文件对话框中,过滤我需要的文件类型。
请教的问题如下:
1。我上面语句运行,不能把目录显示出来。
2。我如何可以实现多个文件类型过滤,例如:*.html,*.xml.等    

解决方案 »

  1.   

    我在 JAVA2 SWING 中看到JFileChooser的一个方法addChoosableFileFilter 
    为什么在JB5中没有呢?
      

  2.   

    JFileChooser jfc = new JFileChooser();
    jfc.setDialogTitle("**********"); 
    System.out.println("ssss");

    //JFileChooser   chooser   =   new   JFileChooser(); 
         //   chooser.setCurrentDirectory(new File( ". ")); 
    jfc.addChoosableFileFilter(new    FileFilter() 
              { 
                public   boolean   accept(File   f) 
                    { 
                      return   f.getName().toLowerCase().endsWith( ".txt "); 
                      } 
                  public   String   getDescription() 
                    { 
                   System.out.println("ssssssss");
                      return   "文本文档(*.txt) "; 
                  
                    } 
              } 
            );   
     
    //得到路径
    jfc.showSaveDialog(null);
    jfc.setVisible(true);
      

  3.   

    我不知道你为什么写这句
    chooser.setCurrentDirectory(new   File( ". ")); 
    我用以上实现了 可以看到目录呀 
      

  4.   

    jfc.addChoosableFileFilter(new    FileFilter() 
              { 
                public   boolean   accept(File   f) 
                    { 
                      return   f.getName().toLowerCase().endsWith( ".xml "); 
                      } 
                  public   String   getDescription() 
                    { 
                   System.out.println("ssssssss");//调试
                      return   "文本文档(*.xml) "; 
                  
                    } 
              } 
            );