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

StringselectDirectory(Component component, String name, File pathToUse)
select Directory
JFileChooser chooser = new JFileChooser();
boolean useIndicated = pathToUse != null;
if (useIndicated && lastIndicatedPath == null) {
    lastIndicatedPath = pathToUse;
chooser.setCurrentDirectory(useIndicated ? lastIndicatedPath : lastChooserPath);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = chooser.showDialog(component, name);
...
File[]selectedFiles(JFileChooser chooser)
Work around Java Bug 4437688 "JFileChooser.getSelectedFile() returns nothing when a file is selected" [Jon Aquino]
return ((chooser.getSelectedFiles().length == 0) && (chooser.getSelectedFile() != null))
        ? new File[] { chooser.getSelectedFile() }
        : chooser.getSelectedFiles();
StringselectFile(boolean exitOnCancel)
Opens up a dialog box asking the user to select a file.
String fileName = null;
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
    fileName = fc.getSelectedFile().getAbsolutePath();
} else if (exitOnCancel) {
    System.exit(0);
return fileName;
FileselectFile(Component parent, boolean isOpen)
select File
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int option = isOpen ? chooser.showOpenDialog(parent) : chooser.showSaveDialog(parent);
if (option != JFileChooser.APPROVE_OPTION)
    return null;
java.io.File folder = chooser.getSelectedFile();
if (folder == null)
    return null;
...
FileselectFile(String msg, File dir, FileFilter filterOrNull)
Pick a file.
if (!isInteractive()) {
    return null;
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(dir);
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int ok = fc.showDialog(null, msg);
if (ok == JFileChooser.APPROVE_OPTION) {
...
FileselectFileForOpen(final Component parent, final FileFilter[] fileFilters, final String title, final FileFilter[] selectedFilters, final File initFile)
select File For Open
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false);
chooser.setDragEnabled(false);
chooser.setControlButtonsAreShown(true);
chooser.setDialogType(JFileChooser.OPEN_DIALOG);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
for (final FileFilter fileFilter : fileFilters) {
    chooser.addChoosableFileFilter(fileFilter);
...
StringselectNonExistingFile(Component parent, String extensionWanted)
select Non Existing File
String forReturn = null;
final String endWith = extensionWanted;
JFileChooser chooser = new JFileChooser(lastChooserPath);
chooser.setFileFilter(new FileFilter() {
    @Override
    public boolean accept(File file) {
        String filename = file.getName();
        return (filename.endsWith(endWith) || file.isDirectory());
...
voidsetCurrentDirectory(JFileChooser fileChooser, String location)
set Current Directory
if (pointsToFile(location))
    location = location.substring(0, location.lastIndexOf("\\"));
File dir = new File(location);
if (dir.exists())
    fileChooser.setCurrentDirectory(dir);
voidsetFile(File file, JFileChooser chooser)
Set JFileChooser so it will always make the best of going to the location of the input file (whether its a file or directory)
if (file == null) {
    return;
if (file.exists() == false && file.isFile()) {
    file = file.getParentFile();
if (file.exists()) {
    if (file.isFile()) {
...
voidsetFileChooserFilters(JFileChooser fileDlg, String filters, int selectIndex)
set File Chooser Filters
if (fileDlg == null) {
    throw new IllegalArgumentException("fileDlg");
FileFilter selectFilter = null;
String[] filterParts = filters.split("[|]", -1);
int filtersNum = filterParts.length / 2;
for (int i = 0; i < filtersNum; i++) {
    int idx = i * 2;
...