I’m constructing a JFileFilter on a JFileChooser, however an error occurs where I have the code setFileFilter and my IDE provides the information ‘The method setFileFilter(FileFilter) in the type JFileChooser is not applicable for the arguments (new FileFilter(){})’. However I’m sure I’ve done it in this format before and it has been fine. Can anyone help me to find and understand why this isn’t working? Thanks!
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Open File");
chooser.setFileFilter(new FileFilter(){
@Override
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)";
}
});
It’s not JFileFilter, it’s just FileFilter
The problem is that there are two FileFilter classes / interfaces in J2SE. One is used in java.io … I’m pretty sure its in File. The other is for the JFileChooser. You have probably imported the wrong one.
BTW, there’s an extension filenameFilter or something like that that probably does everything you want. Find the right class in the docs and look for its subclasses