Java problem - setFileFilter

I am creating a JFileFilter on JFileChooser, however an error occurs when I have the code setFileFilter

and my IDE provides information. The setFileFilter (FileFilter) method of type JFileChooser is not applicable for arguments (new FileFilter () {}) "However, I'm sure I've done it in this format before and everything was fine. Can anyone help me find and understand why doesn't this work? Thanks!

JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Open File");
        chooser.setFileFilter(new FileFilter(){

            public boolean accept(File f) {
                String fName = f.getName().toUpperCase();
                if (fName.endsWith(".TXT") || f.isDirectory()) {
                    return true;
                } else {
                    return false;   

            public String getDescription() {
                return "Text File (*.txt)";




source to share

1 answer

It's not a JFileFilter, it's just a FileFilter

The problem is that there are two FileFilter classes / interfaces in J2SE. One of them is used in ... I'm sure it's in the file. The other is for JFileChooser. You probably imported the wrong one.

By the way, there is a filenameFilter extension or something that probably does whatever you want. Find the class you want in the docs and find its subclasses



All Articles