Here you can find the source of selectFile(String msg, File dir, FileFilter filterOrNull)
Parameter | Description |
---|---|
msg | a parameter |
dir | The starting directory. null for the user's directory |
public static File selectFile(String msg, File dir, FileFilter filterOrNull)
//package com.java2s; //License from project: Open Source License import java.awt.GraphicsEnvironment; import java.io.File; import java.io.FileFilter; import javax.swing.JFileChooser; public class Main { public static File selectFile(String msg, File dir) { return selectFile(msg, dir, null); }/*w w w .ja v a2s .c o m*/ /** * Pick a file. Note: This must be a file, not a directory. * @param msg * @param dir * The starting directory. null for the user's directory * @return selected file or null */ public static File selectFile(String msg, File dir, FileFilter filterOrNull) { if (!isInteractive()) { return null; } JFileChooser fc = new JFileChooser(); fc.setCurrentDirectory(dir); fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // fc.setAcceptAllFileFilterUsed(true); // if (regexFilter!=null) // fc.setFileFilter(FileUtils.getRegexFilter(regexFilter)); int ok = fc.showDialog(null, msg); if (ok == JFileChooser.APPROVE_OPTION) { File f = fc.getSelectedFile(); if (f == null) return null; if (filterOrNull != null && !filterOrNull.accept(f)) { // The Java file-picker makes it easy to choose a directory by mistake return selectFile(msg, f, filterOrNull); } return f; } return null; } /** * @return true if there is a user you can ask for input. true by * default. * <p> * This is set by the environment variable WINTERWELL_HEADLESS, with * WINTERWELL_HEADLESS=true meaning interactive is false. See * http://blog.plover.com/prog/sh-flags.html * <p> * If not set, it uses {@link GraphicsEnvironment#isHeadless()} */ public static boolean isInteractive() { String envVar = System.getProperty("WINTERWELL_HEADLESS"); if (envVar == null) return !GraphicsEnvironment.isHeadless(); return !envVar.toLowerCase().equals("true"); } }