Java Utililty Methods JFileChooser

List of utility methods to do JFileChooser

Description

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

Method

FilegetOpenFile(String name, String currentDirectory)
get Open File
return getOpenFile(name, currentDirectory, null);
FilegetOpenFile(String title)
get Open File
JFileChooser chooser = getChooser(title);
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
    File file = chooser.getSelectedFile();
    System.out.println("User chose file " + file.getAbsolutePath());
    if (!file.exists()) {
        showMessage("File " + file.getName() + " doesn't exist, aborting");
        return null;
...
FilegetPropertiesFile(boolean saving, String startName, String extension, String description)
Opens a file chooser and returns the selected file for open or close operations
JFileChooser fc = createFileChooser(true, startName, saving);
final String finalExtension = extension;
final String finalDescription = description;
if (extension != null && extension.length() > 0) {
    FileFilter ff = new FileFilter() {
        public boolean accept(File f) {
            return f.isDirectory() || f.getName().toLowerCase().endsWith(finalExtension);
        public String getDescription() {
            return finalDescription;
    };
    fc.setFileFilter(ff);
int returnVal = -1;
if (saving) {
    returnVal = fc.showSaveDialog(null);
} else {
    returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
    if (fc.getSelectedFile().getAbsolutePath().endsWith("." + finalExtension)) {
        return fc.getSelectedFile();
    } else {
        return new File(fc.getSelectedFile().getAbsolutePath() + "." + finalExtension);
} else {
    return null;
FilegetSaveAsFile(String defaultName, String currentDirectory, String defaultExtension)
get Save As File
String name = defaultName;
if (defaultName == null)
    name = "data";
if (name.indexOf(".") == -1)
    name += "." + defaultExtension;
if (currentDirectory == null)
    currentDirectory = System.getProperty("user.dir");
return getSaveAsFile(name, currentDirectory);
...
FilegetSaveFile(String message, File defaultFileOrDir, String description, final String... extensions)
get Save File
File[] files = getFile(message, false, defaultFileOrDir, false, description, extensions);
if (files == null) {
    return null;
} else {
    return files[0];
File[]getSelectedFiles(final JFileChooser chooser)
Workaround for Swing bug: JFileChooser does not support multi-file selection See Sun bug database 4218431.
Container c1 = (Container) chooser.getComponent(3);
JList list = null;
while (c1 != null) {
    final Container c = (Container) c1.getComponent(0);
    if (c instanceof JList) {
        list = (JList) c;
        break;
    c1 = c;
final Object[] entries = list.getSelectedValues();
final File[] files = new File[entries.length];
for (int k = 0; k < entries.length; k++) {
    if (entries[k] instanceof File) {
        files[k] = (File) entries[k];
return files;
File[]getSelectedFiles(JFileChooser chooser)
Workaround for Swing bug: JFileChooser does not support multi-file selection See Sun bug database 4218431.
Container c1 = (Container) chooser.getComponent(3);
JList list = null;
while (c1 != null) {
    Container c = (Container) c1.getComponent(0);
    if (c instanceof JList) {
        list = (JList) c;
        break;
    c1 = c;
Object[] entries = list.getSelectedValues();
File[] files = new File[entries.length];
for (int k = 0; k < entries.length; k++) {
    if (entries[k] instanceof File)
        files[k] = (File) entries[k];
return files;
FilegetSelectedFileWithExtension(JFileChooser c)
Works around a JFileChooser limitation, that the selected file when saving is returned exactly as typed and doesn't take into account the selected file filter.
File file = c.getSelectedFile();
if (c.getFileFilter() instanceof FileNameExtensionFilter) {
    String[] exts = ((FileNameExtensionFilter) c.getFileFilter()).getExtensions();
    String nameLower = file.getName().toLowerCase();
    for (String ext : exts) { 
        if (nameLower.endsWith('.' + ext.toLowerCase())) {
            return file; 
    file = new File(file.toString() + '.' + exts[0]);
return file;
FilegetSelectedFileWithExtension(JFileChooser c)
Returns the selected file from a JFileChooser, including the extension from the file filter.
File file = c.getSelectedFile();
if (c.getFileFilter() instanceof FileNameExtensionFilter) {
    String[] exts = ((FileNameExtensionFilter) c.getFileFilter()).getExtensions();
    String nameLower = file.getName().toLowerCase();
    for (String ext : exts) { 
        if (nameLower.endsWith('.' + ext.toLowerCase())) {
            return file; 
    file = new File(file.toString() + '.' + exts[0]);
return file;
FilegetSystemFile(Component owner, int mode, FileFilter[] filters)
Consistent way to chosing a file to open with JFileChooser.
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(mode);
jfc.setFileHidingEnabled(true);
jfc.setAcceptAllFileFilterUsed(true);
if (filters != null) {
    for (int i = 0; i < filters.length; i++) {
        jfc.addChoosableFileFilter(filters[i]);
    if (filters.length >= 1) {
        jfc.setFileFilter(filters[0]);
int result = jfc.showOpenDialog(owner);
if (result == JFileChooser.APPROVE_OPTION) {
    return jfc.getSelectedFile();
return null;