Java JFileChooser selectFile(String msg, File dir, FileFilter filterOrNull)

Here you can find the source of selectFile(String msg, File dir, FileFilter filterOrNull)

Description

Pick a file.

License

Open Source License

Parameter

Parameter Description
msg a parameter
dir The starting directory. null for the user's directory

Return

selected file or null

Declaration

public static File selectFile(String msg, File dir, FileFilter filterOrNull) 

Method Source Code

//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");
    }
}

Related

  1. selectAndSaveToFile(String content, Component parent)
  2. selectDirectory(Component component, String name, File pathToUse)
  3. selectedFiles(JFileChooser chooser)
  4. selectFile(boolean exitOnCancel)
  5. selectFile(Component parent, boolean isOpen)
  6. selectFileForOpen(final Component parent, final FileFilter[] fileFilters, final String title, final FileFilter[] selectedFilters, final File initFile)
  7. selectNonExistingFile(Component parent, String extensionWanted)
  8. setCurrentDirectory(JFileChooser fileChooser, String location)
  9. setFile(File file, JFileChooser chooser)