是这样的,我想在用FileChooser选择打开文件的时候做个文件过滤,比如我想打开.txt的文件,就这样,但是我试了一些方法好像不行,知道的能告诉我下么?,我的代码贴出来:知道的帮我跑出来啊,要跑出来的,谢谢。
@Override
public void mouseClicked(MouseEvent e) {
 doOpenfile();
} private void doOpenfile() {
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);   
fileChooser.setDialogTitle("打开文件夹");   
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int ret = fileChooser.showOpenDialog(null);   
if (ret == JFileChooser.APPROVE_OPTION) {   
//文件夹路径   
System.out.println(fileChooser.getSelectedFile().getAbsolutePath()); 
jtfpath.setText(fileChooser.getSelectedFile().getAbsolutePath());
  
}
}
 

解决方案 »

  1.   

    写个class Filter extends FileFilter
    覆盖里面的accept方法和getDescription方法
    然后fileChooser.addChoosableFileFilter(new Filter())
      

  2.   


      fileChooser.setFileFilter(new FileFilter(){
        @Override
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }
            String ext = null;
            String s = f.getName();
            int i = s.lastIndexOf('.');         if (i > 0 &&  i < s.length() - 1) {
                ext = s.substring(i+1).toLowerCase();
            }
            if (ext != null) {
                if (ext.equals("txt")) {
                        return true;
                } else {
                    return false;
                }
            }
            return false;
        }
        //The description of this filter
        public String getDescription() {
            return "Just Txt";
        }
      });
      

  3.   


    package com.spdb.swing;import java.io.File;
    import javax.swing.filechooser.*;/* ImageFilter.java is used by FileChooserDemo2.java. */
    public class TextFilter extends FileFilter {
    /*
         * Get the extension of a file.
         */
        public static String getExtension(File f) {
            String ext = null;
            String s = f.getName();
            int i = s.lastIndexOf('.');        if (i > 0 &&  i < s.length() - 1) {
                ext = s.substring(i+1).toLowerCase();
            }
            return ext;
        }
        
        //Accept all directories and all gif, jpg, tiff, or png files.
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }        String extension = getExtension(f);
            if (extension != null) {
                if (extension.equals("txt") ||
                    extension.equals("dat")) {
                        return true;
                } else {
                    return false;
                }
            }        return false;
        }    //The description of this filter
        public String getDescription() {
            return "Just Text Files";
        }
    }
    使用方法:// Show it.
    int returnVal = fc.showDialog(RelationImportPanel.this, "打开"); // Process the results.
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fc.getSelectedFile();
    txtFilePath.setText(file.getAbsolutePath());
    log.debug("Attaching file: " + file.getName() + ".");
    } else {
    log.debug("Attachment cancelled by user.");
    } // Reset the file chooser for the next time it's shown.
    fc.setSelectedFile(null);
      

  4.   

    看看 官网的教程
    http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html