Java Utililty Methods JFileChooser Filter

List of utility methods to do JFileChooser Filter

Description

The list of methods to do JFileChooser Filter are organized into topic(s).

Method

booleanaccept(File f, Collection filters)
Determines whether this file is accepted by the specified collection of filters
if (filters == null || filters.isEmpty())
    return true;
for (FileFilter filter : filters)
    if (filter.accept(f))
        return true;
return false;
FilecompleteFileExtension(File file, FileFilter filter)
complete File Extension
if ((file != null) && (filter != null)) {
    if (filter instanceof FileNameExtensionFilter) {
        String[] exts = ((FileNameExtensionFilter) filter).getExtensions();
        String fName = file.getName();
        if ((exts != null) && (fName != null)) {
            int dotPos = fName.indexOf('.');
            if ((exts.length == 1) && ((dotPos < 0) || (dotPos == (fName.length() - 1)))) {
                if (dotPos < 0) {
...
FileFiltercreateFileFilter(final String description, final String[] extensions)
create File Filter
return new FileFilter() {
    @Override
    public String getDescription() {
        return description;
    @Override
    public boolean accept(File f) {
        if (!f.canRead())
...
FileFiltercreateFileFilter(final String description, String extension)
Example usage:
 SwingUtil.createFileFilter("JEQL script (*.jql)", "jql") 
final String dotExt = extension.startsWith(".") ? extension : "." + extension;
FileFilter ff = new FileFilter() {
    public String getDescription() {
        return description;
    public boolean accept(File f) {
        return f.isDirectory() || f.toString().toLowerCase().endsWith(dotExt);
};
return ff;
FileFiltercreateFileFilter(final String ext, final String desc)
create File Filter
return createFileFilter(new String[] { ext }, desc);
FileFiltercreateFileFilter(final String filter)
create File Filter
return new FileFilter() {
    public boolean accept(File f) {
        String ext = "";
        String path = f.getPath();
        int idx = path.lastIndexOf('.');
        if (idx > 0) {
            ext = path.substring(idx + 1).toLowerCase();
            if (ext.equals(filter))
...
FileFiltercreateFileFilter(final String filterName, final String... extensionPatterns)
Creates file filter for files with specified extensions and filter name.
return new FileFilter() {
    @Override
    public boolean accept(final File f) {
        if (f.isDirectory()) {
            return true;
        final String path = f.getAbsolutePath();
        for (final String extension : extensionPatterns) {
...
FileNameExtensionFiltercreateFileFilter(String text, String[]... suffixes)
create File Filter
SortedSet<String> sortedSuffixes = new TreeSet<>();
if (suffixes != null) {
    for (String[] a : suffixes) {
        if (a != null) {
            for (String s : a) {
                if (s != null) {
                    sortedSuffixes.add(s.toLowerCase());
if (!sortedSuffixes.isEmpty()) {
    StringBuilder buf = new StringBuilder(256);
    buf.append(text);
    boolean isFirst = true;
    for (String s : sortedSuffixes) {
        if (isFirst) {
            buf.append(" (");
            isFirst = false;
        } else {
            buf.append("; ");
        buf.append("*.");
        buf.append(s);
    if (!isFirst) {
        buf.append((char) ')');
    text = buf.toString();
return new FileNameExtensionFilter(text, sortedSuffixes.toArray(new String[sortedSuffixes.size()]));
booleanfileEndsInValidExtension(File file, FileNameExtensionFilter filter)
file Ends In Valid Extension
for (String ext : filter.getExtensions()) {
    if (file.toString().endsWith("." + ext)
            || (ext.equals(WILDCARD) && file.toString().matches(FILE_NAME_WITH_EXTENSION_REGEX))) {
        return true;
return false;
StringfileNameAutoExtend(FileFilter filter, File file)
If the user does not input the extension specified by the file filter, automatically augment the file name with the specified extension.
if (filter == null)
    return file.getAbsolutePath();
String description = filter.getDescription().toLowerCase();
String extension = getExtensionInLowerCase(file);
String filename = file.getAbsolutePath();
if (extension != null) {
    if (!filter.accept(file)) {
        filename = file.getAbsolutePath().concat(".").concat(description);
...