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

FilegetMostRecentFile(File dir, java.io.FileFilter filter)
Find the youngest file in the given directory that matches the given FileFilter .
if (!dir.isDirectory()) {
    throw new IllegalArgumentException("Not a directory:" + dir);
File[] list = ((filter == null) ? dir.listFiles() : dir.listFiles(filter));
File latestFile = null;
long latest = Long.MIN_VALUE;
for (int i = 0; i < list.length; i++) {
    long tmp = list[i].lastModified();
...
FileFiltergetWavFileFilter()
get Wav File Filter
return new FileFilter() {
    @Override
    public boolean accept(File f) {
        return f.isDirectory() || f.getName().endsWith(".wav");
    @Override
    public String getDescription() {
        return "WAV sound files";
...
voidlist(ArrayList list, File file, javax.swing.filechooser.FileFilter filter)
list
if (filter.accept(file)) {
    list.add(file);
    if (file.isFile()) {
        return;
if (file.isDirectory()) {
    File files[] = file.listFiles();
...
File[]listAll(File file, javax.swing.filechooser.FileFilter filter)
list All
List<File> arrayList = new ArrayList<>();
File[] files;
if (!file.exists() || file.isFile()) {
    return null;
list(arrayList, file, filter);
files = new File[arrayList.size()];
arrayList.toArray(files);
...
File[]listAll(File file, javax.swing.filechooser.FileFilter filter)
list All
ArrayList<File> list = new ArrayList<File>();
File[] files;
if (!file.exists() || file.isFile()) {
    return null;
list(list, file, filter);
files = new File[list.size()];
list.toArray(files);
...
FileFiltermakeFileFilter(final String desc, final String... types)
make File Filter
return new FileFilter() {
    public boolean accept(File file) {
        if (file.isDirectory()) {
            return true;
        for (String extension : types) {
            if (file.getName().toLowerCase().endsWith(extension)) {
                return true;
...
FileNameExtensionFiltermakeFileNameExtensionFilter(String[] extensions)
make File Name Extension Filter
if (extensions == null)
    return null;
int numExtensions = 0;
for (int ii = 0; ii < extensions.length; ++ii)
    if (extensions[ii] != null)
        numExtensions = numExtensions + 1;
String[] nne = new String[numExtensions];
int iext = 0;
...
javax.swing.filechooser.FileFilternewFileFilter(final String desc, final String[] allowed_extensions)
new File Filter
return new javax.swing.filechooser.FileFilter() {
    @Override
    public boolean accept(java.io.File f) {
        if (f.isDirectory()) {
            return true;
        int pos = f.getName().lastIndexOf('.');
        if (pos == -1) {
...
FilevalidateFileName(final File file, final javax.swing.filechooser.FileFilter filter)
Validates the name of a given file, i.e., if the file name doesn't have the proper ending, it will be appended.
if (file == null || filter == null || filter.accept(file)) {
    return file;
String fileName = file.getName();
int index = fileName.lastIndexOf(".");
if (index > 0) {
    fileName = fileName.substring(0, index);
final String extension = filter.toString();
if (extension.matches("(\\w)*")) {
    String newFileName = fileName + "." + extension;
    File newFile = new File(file.getParent(), newFileName);
    return newFile;
return file;
java.io.FileFilterwrapFilter(final javax.swing.filechooser.FileFilter filter)
Create a javaio FileFilter from the filechooser package file filter.
return new java.io.FileFilter() {
    public boolean accept(File f) {
        return ((filter == null) ? true : filter.accept(f));
};