Opens a file with a FileChooser dialog. - Java Swing

Java examples for Swing:JDialog

Description

Opens a file with a FileChooser dialog.

Demo Code

/*//from   w  w  w .  ja  v a 2s . c o m
 * Created on 31-Oct-2004 at 20:29:10.
 * 
 * Copyright (c) 2004-2005 Robert Virkus / Enough Software
 *
 * This file is part of J2ME Polish.
 *
 * J2ME Polish 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.
 * 
 * J2ME Polish 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 J2ME Polish; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Commercial licenses are also available, please
 * refer to the accompanying LICENSE.txt or visit
 * http://www.j2mepolish.org for details.
 */
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileFilter;

public class Main{
    /**
     * Opens a file with a FileChooser dialog.
     * 
     * @param extension the needed extension, e.g. ".java"
     * @param open true when a file should be opened, if false is given, the save-Dialog is shown instead
     * @param currentDirectory the current directory
     * @param parent the frame which wants to show the FileChooser
     * @return the chosen file or null, when cancel was selected
     */
    public static File openFile(String extension, boolean open,
            File currentDirectory, JFrame parent) {
        JFileChooser fileChooser = new JFileChooser(currentDirectory);
        if (extension != null) {
            fileChooser.setFileFilter(new CustomFileFilter(extension));
        }
        int result;
        if (open) {
            result = fileChooser.showOpenDialog(parent);
        } else {
            result = fileChooser.showSaveDialog(parent);
        }
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            if (!open && extension != null) {
                if (!selectedFile.getName().endsWith(extension)) {
                    selectedFile = new File(selectedFile.getAbsolutePath()
                            + extension);
                }
            }
            return selectedFile;
        } else {
            return null;
        }
    }
}

Related Tutorials