Java JFileChooser chooseFiles(String title, File currentDir)

Here you can find the source of chooseFiles(String title, File currentDir)

Description

Open a javax.swing.JFileChooser with multi-selection enabled, and return the selected file(s), or null if closed or cancelled.

License

Apache License

Parameter

Parameter Description
title The title of the file-chooser window.
currentDir The root directory. If null, new File(".") will be used.

Return

The chosen file(s), or null if none was chosen.

Declaration

public static File[] chooseFiles(String title, File currentDir) 

Method Source Code


//package com.java2s;
/*/*from  ww  w. j a v  a 2 s .  c  o  m*/
 * Copyright 2014 Aritz Lopez
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

import javax.swing.JFileChooser;
import java.io.File;

public class Main {
    /**
     * Open a {@link javax.swing.JFileChooser} with multi-selection enabled, and return the selected file(s), or null if closed or cancelled.
     *
     * @param title      The title of the file-chooser window.
     * @param currentDir The root directory. If null, {@code new File(".")} will be used.
     * @return The chosen file(s), or null if none was chosen.
     */
    public static File[] chooseFiles(String title, File currentDir) {
        if (currentDir == null)
            currentDir = new File(".");
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setCurrentDirectory(currentDir);
        fileChooser.setDialogTitle(title);
        fileChooser.setMultiSelectionEnabled(true);
        int result = fileChooser.showOpenDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) {
            return fileChooser.getSelectedFiles();
        }
        return null;
    }
}

Related

  1. ChooseFile(final String desc, final String[] allowed_extensions, String suggested_dir)
  2. chooseFile(JFileChooser fc, Component parent)
  3. chooseFile(String title)
  4. chooseFiles()
  5. chooseFiles(final Component parent)
  6. chooseFilesForOpen(String title, FileFilter filter, File initialDir)
  7. chooseFileToRead(Component owner, FileFilter filter, File pathToNavigateTo)
  8. chooseNewFile(final Component parent, final File startingDirectory)
  9. chooseOpenFile(String dir, String fileName, String fileNameExtension, Component parent)