Java JFileChooser getSystemFile(Component owner, int mode, FileFilter[] filters)

Here you can find the source of getSystemFile(Component owner, int mode, FileFilter[] filters)

Description

Consistent way to chosing a file to open with JFileChooser.

License

Mozilla Public License

Parameter

Parameter Description
owner to show the component relative to.
mode selection mode for the JFileChooser.

Return

File based on the user selection can be null.

Declaration

public static File getSystemFile(Component owner, int mode, FileFilter[] filters) 

Method Source Code


//package com.java2s;
/*//from  w  w w  . j  a v  a 2 s  .c  o  m
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */

import java.awt.Component;

import java.io.File;

import javax.swing.JFileChooser;

import javax.swing.filechooser.FileFilter;

public class Main {
    /**
     * Consistent way to chosing a file to open with JFileChooser.
     * <p>
     * 
     * @see JFileChooser#setFileSelectionMode(int)
     * @see #getSystemFiles(Component, int)
     * @param owner to show the component relative to.
     * @param mode selection mode for the JFileChooser.
     * @return File based on the user selection can be null.
     */
    public static File getSystemFile(Component owner, int mode, FileFilter[] filters) {

        JFileChooser jfc = new JFileChooser();
        jfc.setFileSelectionMode(mode);
        jfc.setFileHidingEnabled(true);
        jfc.setAcceptAllFileFilterUsed(true);
        if (filters != null) {
            for (int i = 0; i < filters.length; i++) {
                jfc.addChoosableFileFilter(filters[i]);
            }

            if (filters.length >= 1) {
                jfc.setFileFilter(filters[0]);
            }
        }

        int result = jfc.showOpenDialog(owner);

        if (result == JFileChooser.APPROVE_OPTION) {
            return jfc.getSelectedFile();
        }

        return null;
    }

    public static File getSystemFile(Component owner, int mode) {

        return getSystemFile(owner, mode, null);
    }
}

Related

  1. getSaveFile(String message, File defaultFileOrDir, String description, final String... extensions)
  2. getSelectedFiles(final JFileChooser chooser)
  3. getSelectedFiles(JFileChooser chooser)
  4. getSelectedFileWithExtension(JFileChooser c)
  5. getSelectedFileWithExtension(JFileChooser c)
  6. getTextFileChooser()
  7. getXmlFileChooser()
  8. importFile(JFileChooser jFileChooser)
  9. initFileChooser(JFileChooser fileChooser, FileFilter filter)