JFileChooser chooser = new JFileChooser();
// using in-line definition (anonymous) implementation.
chooser.addChoosableFileFilter(new FileFilter() {
@Override
public boolean accept(File arg0) {
return true;
}
@Override
public String getDescription() {
return null;
}
});
// using a pre-defined (named) class implementation.
chooser.addChoosableFileFilter(
new SomeCustomFileFilter()
);
As shown in the code above, the key method is addChoosableFileFilter(javchooser.setAcceptAllFileFilterUsed(false);
public String getDescription() {
return "*.RTF";
}
Tired yet!
public boolean accept(File f) {
// Allow directories to be seen.
if (f.isDirectory()) return true;
// Allows files with .rtf extension to be seen.
if(f.getName().toLowerCase().endsWith(".rtf"))
return true;
// Otherwise file is not shown.
return false;
}
VoilĂ !
f.getName().toLowerCase().endsWith(".rtf")
".rtf".equalsIgnoreCase(fileext)
/**
* Inherited FileFilter class to facilitate reuse when
* multiple file filter selections are required. For example
* purposes, I used a static nested class, which is defined
* as below as a member of our original FileChooserExample
* class.
*/
static class ExtensionFileFilter
extends javax.swing.filechooser.FileFilter {
private java.util.List<String> extensions;
private String description;
public ExtensionFileFilter(String[] exts, String desc) {
if (exts != null) {
extensions = new java.util.ArrayList<String>();
for (String ext : exts) {
// Clean array of extensions to remove "."
// and transform to lowercase.
extensions.add(
ext.replace(".", "").trim().toLowerCase()
);
}
} // No else need; null extensions handled below.
// Using inline if syntax, use input from desc or use
// a default value.
// Wrap with an if statement to default as well as
// avoid NullPointerException when using trim().
description = (desc != null) ? desc.trim() : "Custom File List";
}
// Handles which files are allowed by filter.
@Override
public boolean accept(File f) {
// Allow directories to be seen.
if (f.isDirectory()) return true;
// exit if no extensions exist.
if (extensions == null) return false;
// Allows files with extensions specified to be seen.
for (String ext : extensions) {
if (f.getName().toLowerCase().endsWith("." + ext))
return true;
}
// Otherwise file is not shown.
return false;
}
// 'Files of Type' description
@Override
public String getDescription() {
return description;
}
}
public static void main(String[] args) {
// Instantiate the javax.swing.JFileChooser object.
JFileChooser chooser = new JFileChooser();
// Add file filter to the chooser object,
// using custom implementation of abstract
// javax.swing.filechooser.FileFilter.
chooser
.addChoosableFileFilter(new ExtensionFileFilter(
new String[] { ".DOC", ".RTF", ".TXT" },
"Text Documents (*.DOC|RTF|TXT)"));
// Add second file filter to the chooser object,
// using our same implementation class with different
// constructor parameters.
chooser
.addChoosableFileFilter(new ExtensionFileFilter(
new String[] { ".CSV" },
"Comma Delimited File (*.CSV)"));
// Verify errant inputs are handled correctly.
chooser
.addChoosableFileFilter(new ExtensionFileFilter(
null, null));
// Turn off 'All Files' capability of file chooser,
// so only our custom filter is used.
chooser.setAcceptAllFileFilterUsed(false);
chooser.showSaveDialog(null);
}
That's it!
chooser.showOpenDialog(null);
chooser.showDialog(null, "Custom Approve Button Text");
//FileChooserExample.java
package com.experts_exchange;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
/**
* Example of using file filter(s) with JFileChooser to limit files to specific
* extension(s).
*
* @author mwvisa1
*/
public class FileChooserExample {
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
// Instantiate the javax.swing.JFileChooser object.
JFileChooser chooser = new JFileChooser();
// Add file filter to the chooser object,
// using anonymous implementation of abstract
// javax.swing.filechooser.FileFilter.
chooser.addChoosableFileFilter(new FileFilter() {
// Handles which files are allowed by filter.
@Override
public boolean accept(File f) {
// Allow directories to be seen.
if (f.isDirectory()) return true;
// Allows files with .rtf extension to be seen.
if (f.getName().toLowerCase().endsWith(".rtf"))
return true;
// Otherwise file is not shown.
return false;
}
// 'Files of Type' description
@Override
public String getDescription() {
return "*.RTF";
}
});
// Turn off 'All Files' capability of file chooser,
// so only our custom filter is used.
chooser.setAcceptAllFileFilterUsed(false);
// Where null would be replaced by parent Component.
chooser.showSaveDialog(null);
}
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented:
Thanks for the article.
What if we want to filter based on 3 extensions *.xlsx, *.xls, *.csv
Regards