Java JFileChooser chooseFiles()

Here you can find the source of chooseFiles()

Description

choose Files

License

Open Source License

Declaration

public static String[] chooseFiles() 

Method Source Code

//package com.java2s;
/*/*ww  w.ja va  2  s.  co  m*/
 * Copyright (C) 2011 NATSRL @ UMD (University Minnesota Duluth, US) and
 * Software and System Laboratory @ KNU (Kangwon National University, Korea) 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

public class Main {
    public static String[] chooseFiles() {
        return chooseFiles(".", "Open Files");
    }

    public static String[] chooseFiles(String currentPath, String dialogTitle) {
        return chooseFiles(currentPath, dialogTitle, (FileFilter) null);
    }

    public static String[] chooseFiles(String currentPath, String dialogTitle, FileFilter filter) {
        return chooseFileOrDirectorys(currentPath, dialogTitle, JFileChooser.FILES_ONLY,
                new FileFilter[] { filter });
    }

    public static String[] chooseFiles(String currentPath, String dialogTitle, FileFilter[] filters) {
        return chooseFileOrDirectorys(currentPath, dialogTitle, JFileChooser.FILES_ONLY, null);
    }

    private static String[] chooseFileOrDirectorys(String currentPath, String dialogTitle, int mode,
            FileFilter[] filters) {
        JFileChooser chooser = new JFileChooser();
        if (filters != null) {
            for (FileFilter f : filters) {
                chooser.addChoosableFileFilter(f);
            }
        }
        chooser.setCurrentDirectory(new java.io.File(currentPath));
        chooser.setDialogTitle(dialogTitle);
        chooser.setFileSelectionMode(mode);
        chooser.setMultiSelectionEnabled(true);
        chooser.setAcceptAllFileFilterUsed(false);
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            File[] files = chooser.getSelectedFiles();
            String[] filenames = new String[files.length];
            for (int i = 0; i < files.length; i++) {
                filenames[i] = files[i].getAbsolutePath();
            }
            return filenames;
        }
        return null;
    }
}

Related

  1. chooseFile(File initialFile, boolean load)
  2. chooseFile(final Component parent, final boolean filesOnly, final String title, final File selectedFile, final FileFilter filter)
  3. ChooseFile(final String desc, final String[] allowed_extensions, String suggested_dir)
  4. chooseFile(JFileChooser fc, Component parent)
  5. chooseFile(String title)
  6. chooseFiles(final Component parent)
  7. chooseFiles(String title, File currentDir)
  8. chooseFilesForOpen(String title, FileFilter filter, File initialDir)
  9. chooseFileToRead(Component owner, FileFilter filter, File pathToNavigateTo)