Java examples for Swing:JFileChooser
JFileChooser choose Files
/*// w w w. ja v a 2s . com * 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/>. */ //package com.java2s; 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; } }