Here you can find the source of askForFile(String title, File rootDir)
public static File askForFile(String title, File rootDir)
//package com.java2s; /*/*from w w w .j av a 2 s . c om*/ * Copyright (C) 2015 Christopher Collin Hall * * 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 java.util.Arrays; import javax.swing.JFileChooser; import javax.swing.filechooser.FileFilter; public class Main { public static File askForFile(File rootDir) { JFileChooser jfc = new JFileChooser(rootDir); int action = jfc.showOpenDialog(null); if (action == JFileChooser.CANCEL_OPTION) return null; return jfc.getSelectedFile(); } public static File askForFile(String title, File rootDir) { JFileChooser jfc = new JFileChooser(rootDir); jfc.setDialogTitle(title); int action = jfc.showOpenDialog(null); if (action == JFileChooser.CANCEL_OPTION) return null; return jfc.getSelectedFile(); } public static File askForFile(File rootDir, String... allowedSuffixes) { final String[] suffixes = allowedSuffixes; final String description = Arrays.toString(suffixes); JFileChooser jfc = new JFileChooser(rootDir); jfc.setFileFilter(new FileFilter() { @Override public boolean accept(File f) { if (f.isDirectory()) return true; if (f.getName().contains(".") == false) return false; String suffix = f.getName().substring(f.getName().lastIndexOf(".")); for (String s : suffixes) { if (suffix.equalsIgnoreCase(s)) return true; } return false; } @Override public String getDescription() { return description; } }); int action = jfc.showOpenDialog(null); if (action == JFileChooser.CANCEL_OPTION) return null; return jfc.getSelectedFile(); } }