Java JFileChooser createOpenFileChooser(String title, String dir, Component parent, FileFilter filter)

Here you can find the source of createOpenFileChooser(String title, String dir, Component parent, FileFilter filter)

Description

Create and open a single file chooser.

License

Open Source License

Parameter

Parameter Description
title The dialog title.
dir The initial directory.
parent The parent reference.
filter The file filter to use.

Return

The selected media if accepted, null if not.

Declaration

public static File createOpenFileChooser(String title, String dir, Component parent, FileFilter filter) 

Method Source Code


//package com.java2s;
/*//from w  w  w  .jav  a 2 s  .  co m
 * Copyright (C) 2013-2016 Byron 3D Games Studio (www.b3dgs.com) Pierre-Alexandre (contact@b3dgs.com)
 * 
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

import java.awt.Component;

import java.io.File;

import javax.swing.JFileChooser;

import javax.swing.filechooser.FileFilter;

public class Main {
    /**
     * Create and open a single file chooser.
     * 
     * @param title The dialog title.
     * @param dir The initial directory.
     * @param parent The parent reference.
     * @param filter The file filter to use.
     * @return The selected media if accepted, <code>null</code> if not.
     */
    public static File createOpenFileChooser(String title, String dir, Component parent, FileFilter filter) {
        final JFileChooser fileChooser = new JFileChooser(dir);
        fileChooser.setDialogTitle(title);
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
        fileChooser.setMultiSelectionEnabled(false);
        fileChooser.setFileFilter(filter);

        final int approve = fileChooser.showOpenDialog(parent);
        if (approve == JFileChooser.APPROVE_OPTION) {
            return fileChooser.getSelectedFile();
        }
        return null;
    }
}

Related

  1. createFileChooser(String initDir)
  2. createFileChooser(String title, File dir, String filter)
  3. createJFileChooser(String name)
  4. createJFileChooserWithExistenceChecking()
  5. createJFileChooserWithOverwritePrompting()
  6. createReportFileChooser(String curDir, File defaultReportFile)
  7. createUserDefinedProfileFileChooser()
  8. directoryFetch(File starting)
  9. exportFile(JFileChooser fileChooser, String contents)