Java JFileChooser promptFile(File file, FileFilter filter, String title)

Here you can find the source of promptFile(File file, FileFilter filter, String title)

Description

Checks if the specified file exists.

License

Open Source License

Parameter

Parameter Description
file the file to check.
filter the filter to apply in the dialog if the file does not exist. May be null .
title the title to set for the dialog if the file does not exist. May be null .

Exception

Parameter Description
FileNotFoundException if the user refuses to select a file, or selects a non-existent one.

Return

the original file if it exists, or the replacement file if another one had to be selected.

Declaration

public static File promptFile(File file, FileFilter filter, String title) throws FileNotFoundException 

Method Source Code


//package com.java2s;
/*/*w  w w.ja v  a  2  s  .  c  o m*/
 * FileUtilities.java (Class: com.madphysicist.tools.util.FileUtilities)
 *
 * Mad Physicist JTools Project (General Purpose Utilities)
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013 by Joseph Fox-Rabinovitz
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.io.File;

import java.io.FileNotFoundException;

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

public class Main {
    /**
     * Checks if the specified file exists. If it does not, the user is asked to
     * select a replacement from a file chooser dialog.
     *
     * @param filename the name of the file to check.
     * @param filter the filter to apply in the dialog if the file does not exist. May be {@code null}.
     * @return the original file if it exists, or the replacement file if another one had to be selected.
     * @throws FileNotFoundException if the user refuses to select a file, or selects a non-existent one.
     * @since 1.0.0
     */
    public static File promptFile(String filename, FileFilter filter) throws FileNotFoundException {
        return promptFile(filename, filter, null);
    }

    /**
     * Checks if the specified file exists. If it does not, the user is asked to
     * select a replacement from a file chooser dialog.
     *
     * @param filename the name of the file to check.
     * @param filter the filter to apply in the dialog if the file does not exist. May be {@code null}.
     * @param title the title to set fot the dialog if the file does not exist. May be {@code null}.
     * @return the original file if it exists, or the replacement file if another one had to be selected.
     * @throws FileNotFoundException if the user refuses to select a file, or selects a non-existent one.
     * @since 1.0.0
     */
    public static File promptFile(String filename, FileFilter filter, String title) throws FileNotFoundException {
        return promptFile((filename == null) ? null : new File(filename), filter, title);
    }

    /**
     * Checks if the specified file exists. If it does not, the user is asked to
     * select a replacement from a file chooser dialog.
     *
     * @param file the file to check.
     * @param filter the filter to apply in the dialog if the file does not exist. May be {@code null}.
     * @param title the title to set for the dialog if the file does not exist. May be {@code null}.
     * @return the original file if it exists, or the replacement file if another one had to be selected.
     * @throws FileNotFoundException if the user refuses to select a file, or selects a non-existent one.
     * @since 1.0.0
     */
    public static File promptFile(File file, FileFilter filter, String title) throws FileNotFoundException {
        if (file == null || !file.exists()) {
            File start = file.getParentFile();
            if (!start.exists()) {
                start = new File(".");
            }
            JFileChooser chooser = new JFileChooser(start);

            if (title != null) {
                chooser.setDialogTitle(title);
            }
            if (filter != null) {
                chooser.addChoosableFileFilter(filter);
            }
            chooser.setFileFilter(filter);
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            chooser.setMultiSelectionEnabled(false);
            chooser.setAcceptAllFileFilterUsed(true);

            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                file = chooser.getSelectedFile();
            } else {
                file = null;
            }
            if (file == null || !file.exists()) {
                throw new FileNotFoundException((file == null) ? "" : file.getAbsolutePath());
            }
        }

        return file;
    }
}

Related

  1. loadFileAs(Class clazz, String json)
  2. makeFileChooser()
  3. openDataFileChooser(Component com)
  4. openFile()
  5. openFile(String fileExtension)
  6. promptForFilename(String title)
  7. PromptForFileOpen(Component c)
  8. promptSaveJFileChooser(String suggestedFileName, FileNameExtensionFilter... fileNameExtensionFilters)
  9. readAsString(File file)