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

File[]chooseFilesForOpen(String title, FileFilter filter, File initialDir)
Open-file dialog variant which lets user select multiple files on Windows and Linux.
JFileChooser fd = new JFileChooser();
fd.setDialogTitle(title);
fd.setSelectedFile(initialDir);
fd.setDialogType(JFileChooser.OPEN_DIALOG);
if (filter != null) {
    fd.setFileFilter(filter);
fd.setMultiSelectionEnabled(true);
...
FilechooseFileToRead(Component owner, FileFilter filter, File pathToNavigateTo)
choose File To Read
File result = null;
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(filter);
if (pathToNavigateTo != null) {
    fileChooser.setCurrentDirectory(pathToNavigateTo);
int returnVal = fileChooser.showOpenDialog(owner);
if (returnVal == JFileChooser.APPROVE_OPTION) {
...
FilechooseNewFile(final Component parent, final File startingDirectory)
Shows a single file chooser that accepts new files as input.
JFileChooser chooser;
if (startingDirectory != null && startingDirectory.canRead()) {
    if (startingDirectory.isDirectory()) {
        chooser = new JFileChooser(startingDirectory);
    } else {
        chooser = new JFileChooser(startingDirectory.getParentFile());
} else {
...
FilechooseOpenFile(String dir, String fileName, String fileNameExtension, Component parent)
Opens a file chooser and returns the selected file if available.
JFileChooser chooser = new JFileChooser();
File selectedFile = null;
File fileDir = null;
if (dir == null) {
    fileDir = new File(".");
} else {
    fileDir = new File(dir);
if (!fileDir.exists()) {
    fileDir = new File(".");
chooser.setCurrentDirectory(fileDir);
if (fileName != null) {
    chooser.setSelectedFile(new java.io.File(fileName));
if (fileNameExtension != null) {
    chooser.setFileFilter(new FileNameExtensionFilter(fileNameExtension + " file", fileNameExtension));
chooser.setDialogTitle("Open File");
boolean isValid = true;
do {
    int chooserState = chooser.showDialog(parent, "Ok");
    if (chooserState == JFileChooser.APPROVE_OPTION) {
        selectedFile = chooser.getSelectedFile();
        if (!selectedFile.exists()) {
            JOptionPane.showMessageDialog(parent,
                    selectedFile.getName() + " does not exist. Please choose another file!",
                    "File Not Found", JOptionPane.WARNING_MESSAGE);
        } else {
            isValid = false;
    } else {
        return null;
} while (isValid);
return selectedFile;
FilechooseSaveFile(Component parent, File defaultFile, FileFilter filter)
This method shows a dialog that the user can use to select a file, which it returns.
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(filter);
if (defaultFile != null) {
    chooser.setSelectedFile(defaultFile);
int result = chooser.showSaveDialog(parent);
if (result == JFileChooser.APPROVE_OPTION) {
    return chooser.getSelectedFile();
...
FilechooseSaveFile(String dir, String initialFile, String fileNameExtension, Component parent)
Opens a file chooser and let the user to either choose or specify a file for saving purpose.
JFileChooser chooser = new JFileChooser();
File selectedFile = null;
String selectedFileName = null;
File fileDir = null;
if (dir == null) {
    fileDir = new File(".");
} else {
    fileDir = new File(dir);
...
StringchoseFile(File selectedFile, String startDir, final String description, final String extension, Component parent, boolean addExtension, boolean forLoad)
Opens file chooser.
return choseFile(selectedFile, startDir, description, extension, parent, addExtension, forLoad, true);
voidcleanFileSelector(JFileChooser filterUpdates)
clean File Selector
File f = filterUpdates.getCurrentDirectory();
filterUpdates.setSelectedFile(new File(" "));
filterUpdates.setCurrentDirectory(f);
voidcleanFileSelector(JFileChooser filterUpdates)
set selected file to " ", and current directory to last selected current directory
File f = filterUpdates.getCurrentDirectory();
filterUpdates.setSelectedFile(new File(" "));
filterUpdates.setCurrentDirectory(f);
voidconfigureFileChooser(JFileChooser fchooser, boolean directoryOnly, File selectedFile)
Configures fchooser .
int mode = directoryOnly ? JFileChooser.DIRECTORIES_ONLY : JFileChooser.FILES_ONLY;
fchooser.setFileSelectionMode(mode);
fchooser.setSelectedFile(selectedFile);