Java tutorial
//package com.java2s; /* * 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); } }