Java JFileChooser chooseFile(Component p, String message, boolean toOpen, int fileType)

Here you can find the source of chooseFile(Component p, String message, boolean toOpen, int fileType)

Description

Ask the user to provide a file or directory

License

Open Source License

Declaration

public static File chooseFile(Component p, String message,
        boolean toOpen, int fileType) 

Method Source Code

//package com.java2s;
/**// w w  w . jav  a 2  s.  co  m
 * AC - A source-code copy detector
 *
 *     For more information please visit:  http://github.com/manuel-freire/ac
 *
 * ****************************************************************************
 *
 * This file is part of AC, version 2.0
 *
 * AC is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * AC 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with AC.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;

import java.awt.*;
import javax.swing.*;

public class Main {
    /**
     * Ask the user to provide a file or directory
     */
    public static File chooseFile(Component p, String message,
            boolean toOpen, int fileType) {
        JFileChooser jfc = new JFileChooser();
        jfc.setDialogTitle("Selecciona " + message);
        jfc.setFileSelectionMode(fileType);
        File f = null;
        while (f == null) {
            int rc = (toOpen ? jfc.showOpenDialog(p) : jfc
                    .showSaveDialog(p));
            if (rc == JFileChooser.CANCEL_OPTION) {
                f = null;
                break;
            }

            f = jfc.getSelectedFile();
            if (f == null
                    || (!f.exists() && toOpen)
                    || (fileType == JFileChooser.FILES_ONLY && f
                            .isDirectory())) {
                JOptionPane.showMessageDialog(null, "Error: " + message
                        + " invalido", "Error", JOptionPane.ERROR_MESSAGE);
                f = null;
                continue;
            }
        }
        return f;
    }
}

Related

  1. browseFileForList(DefaultListModel listModel, JFileChooser fileChooser, Component parent)
  2. chooseDirectory(Component parentComponent, File directory, String title)
  3. chooseDirectory(String name, final String desc, File curdir)
  4. chooseFile()
  5. chooseFile()
  6. chooseFile(Component parent, String title, boolean open)
  7. chooseFile(Component parentComponent, String title, File curDir, String suffix)
  8. chooseFile(File initialFile, boolean load)
  9. chooseFile(final Component parent, final boolean filesOnly, final String title, final File selectedFile, final FileFilter filter)