Java JFileChooser getFileOrDir(String startName, boolean fileSelect)

Here you can find the source of getFileOrDir(String startName, boolean fileSelect)

Description

Open a JFileChooser to select a filename or directory starting with startName

License

Open Source License

Parameter

Parameter Description
startName JFileChooser starts at this point

Return

selected filename or dir or an emtpy string

Declaration

public static String getFileOrDir(String startName, boolean fileSelect) 

Method Source Code

//package com.java2s;
/*//from   ww  w .  ja  v a  2  s  . c o m
 * Copyright (C) 2008 Michael Romankiewicz
 * microm at users.sourceforge.net
 *
 * 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 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.io.File;

import javax.swing.JFileChooser;

public class Main {
    /**
     * Open a JFileChooser to select a filename or directory starting with startName 
     * @param startName JFileChooser starts at this point
     * @return selected filename or dir or an emtpy string
     */
    public static String getFileOrDir(String startName, boolean fileSelect) {
        String selection = "";

        JFileChooser fc = createFileChooser(fileSelect, startName, false);
        fc.setMultiSelectionEnabled(false);

        int returnVal = -1;
        returnVal = fc.showOpenDialog(null);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            selection = fc.getSelectedFile().getAbsolutePath();
        }

        return selection;
    }

    /**
     * Create a file chooser
     * @param fileSelect true for using as file selection box, false for using as directory selection box 
     * @param startDirectory initial directory 
     * @param saveDialog true for using as save dialog, false for using as open dialog 
     * @return created file chooser
     */
    private static JFileChooser createFileChooser(boolean fileSelect,
            String startDirectory, boolean saveDialog) {
        if (startDirectory == null) {
            startDirectory = "";
        }
        JFileChooser fc = new JFileChooser(startDirectory);
        fc.setSelectedFile(new File(startDirectory));
        fc.setCurrentDirectory(fc.getSelectedFile());
        fc.enableInputMethods(false);
        fc.setFileHidingEnabled(true);
        if (fileSelect)
            fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        else
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        if (saveDialog)
            fc.setDialogType(JFileChooser.SAVE_DIALOG);
        else
            fc.setDialogType(JFileChooser.OPEN_DIALOG);

        return fc;
    }
}

Related

  1. getFileChooser(Component parent, JFileChooser fileChooser, JComponent accessory)
  2. getFileChooserSelectedFile(JFileChooser fchooser, boolean directoryOnly, String fileName)
  3. getFileFromChooserForPNG()
  4. getFileFromChooserSave()
  5. getFileName(Component parent)
  6. getFilePath()
  7. getFilePath(Component parent, JFileChooser fileChooser, String title, FileFilter filter)
  8. getFiles(String title, String initialRoot, String initialFile)
  9. getFilesToOpen(File startDirectory, Component parent, String... filters)