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

FileaskForFile(String title, File rootDir)
ask For File
JFileChooser jfc = new JFileChooser(rootDir);
jfc.setDialogTitle(title);
int action = jfc.showOpenDialog(null);
if (action == JFileChooser.CANCEL_OPTION)
    return null;
return jfc.getSelectedFile();
File[]askForFiles(File rootDir)
ask For Files
JFileChooser jfc = new JFileChooser(rootDir);
jfc.setMultiSelectionEnabled(true);
int action = jfc.showOpenDialog(null);
if (action == JFileChooser.CANCEL_OPTION)
    return null;
return jfc.getSelectedFiles();
FileaskForFolder(File rootDir)
ask For Folder
JFileChooser jfc = new JFileChooser(rootDir);
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int action = jfc.showOpenDialog(null);
if (action == JFileChooser.CANCEL_OPTION)
    return null;
return jfc.getSelectedFile();
StringaskUserForFileURL(Component aParent, boolean bOpen)
helper to get a file URL selected by user This method doesn't show any API concepts...
String sFileURL = null;
int nDecision = JFileChooser.CANCEL_OPTION;
JFileChooser aChooser = null;
if (maLastDir == null)
    aChooser = new JFileChooser();
else
    aChooser = new JFileChooser(maLastDir);
if (bOpen)
...
voidbrowseFileForList(DefaultListModel listModel, JFileChooser fileChooser, Component parent)
Uses the fileChooser to browse a (not further filtered) file and put the path into the list(-model)
fileChooser.setMultiSelectionEnabled(true);
int returnVal = fileChooser.showOpenDialog(parent);
if (returnVal == JFileChooser.APPROVE_OPTION) {
    for (File file : fileChooser.getSelectedFiles()) {
        listModel.addElement(file.getAbsolutePath());
FilechooseDirectory(Component parentComponent, File directory, String title)
choose Directory
final JFileChooser fileChooser = new JFileChooser(directory.getParentFile());
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.setSelectedFile(directory);
fileChooser.setDialogTitle(title);
if (fileChooser.showSaveDialog(parentComponent) != JFileChooser.APPROVE_OPTION) {
    return null;
return fileChooser.getSelectedFile();
...
FilechooseDirectory(String name, final String desc, File curdir)
A directory-chooser for choosing directories (when file-selecting should be restricted).
JFileChooser fc = new JFileChooser();
fc.setDialogTitle(name);
fc.setAcceptAllFileFilterUsed(false);
fc.setCurrentDirectory(curdir);
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setFileFilter(new FileFilter() {
    @Override
    public boolean accept(File f) {
...
FilechooseFile()
choose File
JFileChooser chooser = new JFileChooser();
File f = null;
FileNameExtensionFilter filter = new FileNameExtensionFilter("Audio Files", "wav", "mp3", "flac");
int returnVal = chooser.showOpenDialog(new JFrame(""));
if (returnVal == JFileChooser.APPROVE_OPTION) {
    f = chooser.getSelectedFile();
    System.out.println("You chose to open this file: " + f.getName());
return f;
StringchooseFile()
Chooses the file which is examined via a simple folder browser Dialog
JFileChooser j = new JFileChooser();
j.setFileSelectionMode(JFileChooser.FILES_ONLY);
j.showOpenDialog(j);
if (j.getSelectedFile() == null) {
    System.out.println("No file was chosen");
} else {
    String file = j.getSelectedFile().getPath();
    return file;
...
FilechooseFile(Component p, String message, boolean toOpen, int fileType)
Ask the user to provide a file or directory
JFileChooser jfc = new JFileChooser();
jfc.setDialogTitle("Selecciona " + message);
jfc.setFileSelectionMode(fileType);
File f = null;
while (f == null) {
    int rc = (toOpen ? jfc.showOpenDialog(p) : jfc.showSaveDialog(p));
    if (rc == JFileChooser.CANCEL_OPTION) {
        f = null;
...