Java JFileChooser chooseFile(Component parent, String title, boolean open)

Here you can find the source of chooseFile(Component parent, String title, boolean open)

Description

Chooses a file to open or save

License

Open Source License

Declaration

public static File chooseFile(Component parent, String title, boolean open) 

Method Source Code


//package com.java2s;
/*//from   w  w  w  .  ja  v a2  s .  co  m
 * ArikTools
 * Copyright (C) Arik Z.Lakritz, Peter Macko, and David K. Wittenberg
 * 
 * This file is part of ArikTools.
 *
 * ArikTools 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 3 of the License, or
 * (at your option) any later version.
 *
 * ArikTools 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 ArikTools.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.*;

import java.io.*;

import javax.swing.*;

public class Main {
    /**
     * Chooses a file to open or save
     */
    public static File chooseFile(Component parent, String title, boolean open) {
        JFileChooser fc = new JFileChooser();
        fc.setDialogTitle(title);
        fc.setDialogType(open ? JFileChooser.OPEN_DIALOG : JFileChooser.SAVE_DIALOG);
        int r = 0;
        if (open) {
            r = fc.showOpenDialog(parent);
        } else {
            r = fc.showSaveDialog(parent);
        }
        if (r != JFileChooser.APPROVE_OPTION)
            return null;
        return fc.getSelectedFile();
    }
}

Related

  1. chooseDirectory(Component parentComponent, File directory, String title)
  2. chooseDirectory(String name, final String desc, File curdir)
  3. chooseFile()
  4. chooseFile()
  5. chooseFile(Component p, String message, boolean toOpen, int fileType)
  6. chooseFile(Component parentComponent, String title, File curDir, String suffix)
  7. chooseFile(File initialFile, boolean load)
  8. chooseFile(final Component parent, final boolean filesOnly, final String title, final File selectedFile, final FileFilter filter)
  9. ChooseFile(final String desc, final String[] allowed_extensions, String suggested_dir)