Custom File Filter : FileFilter « File « Java Tutorial






import java.io.File;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.filechooser.FileFilter;

public class CustomFileFilter extends FileFilter {
  private Hashtable filters = null;

  private String description = null;

  private String fullDescription = null;

  private boolean useExtensionsInDescription = true;

  public CustomFileFilter() {
    this.filters = new Hashtable();
  }

  public CustomFileFilter(String extension) {
    this(extension, null);
  }

  public CustomFileFilter(String extension, String description) {
    this();
    if (extension != null)
      addExtension(extension);
    if (description != null)
      setDescription(description);
  }

  public CustomFileFilter(String[] filters) {
    this(filters, null);
  }

  public CustomFileFilter(String[] filters, String description) {
    this();
    for (int i = 0; i < filters.length; i++) {
      addExtension(filters[i]);
    }
    if (description != null)
      setDescription(description);
  }

  public boolean accept(File f) {
    if (f != null) {
      if (f.isDirectory()) {
        return true;
      }
      String extension = getExtension(f);
      if (extension != null && filters.get(getExtension(f)) != null) {
        return true;
      }
    }
    return false;
  }

  public String getExtension(File f) {
    if (f != null) {
      String filename = f.getName();
      int i = filename.lastIndexOf('.');
      if (i > 0 && i < filename.length() - 1) {
        return filename.substring(i + 1).toLowerCase();
      }
    }
    return null;
  }

  public void addExtension(String extension) {
    if (filters == null) {
      filters = new Hashtable(5);
    }
    filters.put(extension.toLowerCase(), this);
    fullDescription = null;
  }

  public String getDescription() {
    if (fullDescription == null) {
      if (description == null || isExtensionListInDescription()) {
        fullDescription = description == null ? "(" : description + " (";
        Enumeration extensions = filters.keys();
        if (extensions != null) {
          fullDescription += "." + (String) extensions.nextElement();
          while (extensions.hasMoreElements()) {
            fullDescription += ", " + (String) extensions.nextElement();
          }
        }
        fullDescription += ")";
      } else {
        fullDescription = description;
      }
    }
    return fullDescription;
  }

  public void setDescription(String description) {
    this.description = description;
    fullDescription = null;
  }

  public void setExtensionListInDescription(boolean b) {
    useExtensionsInDescription = b;
    fullDescription = null;
  }

  public boolean isExtensionListInDescription() {
    return useExtensionsInDescription;
  }
}








11.72.FileFilter
11.72.1.Create custom File filter
11.72.2.List file with file filter
11.72.3.Custom File Filter
11.72.4.Filter by file
11.72.5.This filter accepts Files that are directories
11.72.6.Filters files based on the extension (what the filename ends with)
11.72.7.Inverted File Filter
11.72.8.Files filter that filters files by their suffix (or extension)
11.72.9.Generic File Filter
11.72.10.A suffix based file filter.