Creates a FileFilter for a specified description and an array of allowed extensions. - Java Swing

Java examples for Swing:JFileChooser

Description

Creates a FileFilter for a specified description and an array of allowed extensions.

Demo Code


//package com.java2s;

import java.io.File;

import javax.swing.filechooser.FileFilter;

public class Main {
    /**/*from  ww w .j a v  a 2s . co m*/
     * Creates a FileFilter for a specified description
     * and an array of allowed extensions. <br />
     * 
     * @param extensions the allowed extensions without a dot
     * @param description the displayed description
     * @return the created FileFilter
     */
    public static FileFilter createFilter(final String[] extensions,
            final String description) {
        return new FileFilter() {

            @Override
            public boolean accept(File file) {
                if (file.isDirectory()) {
                    return true;
                }
                String name = file.getName().toLowerCase();
                for (String e : extensions) {
                    if (name.endsWith("." + e.toLowerCase())) {
                        return true;
                    }
                }
                return false;
            }

            @Override
            public String getDescription() {
                return description;
            }
        };
    }
}

Related Tutorials