Hi, does anyone know how to set a JFileChoosers default filter back to accept all files after custom filters have been added?


Member Avatar for iamthwee

Surely it would be something on the lines of changing the *.txt filter back to "*."

Although that's just me thinking out aloud.

JFileChooser has a setFileFilter(FileFilter filter) method. I could call this by putting any of my custom filters in but i don't have an identifier for the default one. So basically i'm just wonding what the default identifier is.

Member Avatar for iamthwee

Well surely the default filter must be *.*.

Which I would imagine was derived from a regular expression? But I don't have my java compiler at hand to test that. And the API for JFilterChooser may not include that.

but the setFileFilter method requires a FileFilter identifier not a String.

for example:

ExtensionFileFilter filter = new ExtensionFileFilter(text, "*.txt");

perhaps i'm misunderstanding.

Member Avatar for iamthwee


import javax.swing.*;
import java.io.*;

/** Filter to work with JFileChooser to select java file types. **/
public class JavaFilter extends javax.swing.filechooser.FileFilter
  public boolean accept (File f) {
    return f.getName ().toLowerCase ().endsWith ("*.*")
          || f.isDirectory ();
  public String getDescription () {
    return "Java files (*.*)";
} // class JavaFilter

Perhaps? Untested. Although I'd imagine it to be something along those lines?

Ok thanks i'll take a look at the example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.