Java JFileChooser getFileChooserSelectedFile(JFileChooser fchooser, boolean directoryOnly, String fileName)

Here you can find the source of getFileChooserSelectedFile(JFileChooser fchooser, boolean directoryOnly, String fileName)

Description

Generates the selected file for the given JFileChooser based on the directoryOnly and fileName parameters.

License

Open Source License

Parameter

Parameter Description
fchooser the target JFileChooser
directoryOnly if only directories are accepted.
fileName the candidate name for the selected file.

Return

the selected File .

Declaration

public static final File getFileChooserSelectedFile(JFileChooser fchooser, boolean directoryOnly,
        String fileName) 

Method Source Code


//package com.java2s;
/*//from   w  ww .j a va 2s  . c  o m
 * #%L
 * LA-iMageS GUI
 * %%
 * Copyright (C) 2016 Marco Aur?lio Zezzi Arruda, Gustavo de Souza
 *          Pess?a, Jos? Luis Capelo Mart?nez, Florentino Fdez-Riverola, Miguel
 *          Reboiro-Jato, Hugo L?pez-Fdez, and Daniel Glez-Pe?a
 * %%
 * 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 3 of the
 * License, or (at your option) 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, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import java.io.File;
import javax.swing.JFileChooser;

public class Main {
    /**
     * Generates the selected file for the given {@code JFileChooser} based on
     * the {@code directoryOnly} and {@code fileName} parameters.
     * 
     * @param fchooser the target {@code JFileChooser}
     * @param directoryOnly if only directories are accepted.
     * @param fileName the candidate name for the selected file.
     * @return the selected {@code File}.
     */
    public static final File getFileChooserSelectedFile(JFileChooser fchooser, boolean directoryOnly,
            String fileName) {
        File targetSelectedFile;
        File currentSelectedFile = fchooser.getSelectedFile();
        if (currentSelectedFile == null) {
            if (directoryOnly) {
                targetSelectedFile = fchooser.getCurrentDirectory();
            } else {
                targetSelectedFile = new File(fchooser.getCurrentDirectory(), fileName);
            }
        } else {
            if (directoryOnly) {
                targetSelectedFile = currentSelectedFile.isDirectory() ? currentSelectedFile
                        : currentSelectedFile.getParentFile();
            } else {
                File dir = currentSelectedFile.isDirectory() ? currentSelectedFile
                        : currentSelectedFile.getParentFile();
                targetSelectedFile = new File(dir, fileName);
            }
        }
        return targetSelectedFile;
    }
}

Related

  1. getFileChooser()
  2. getFileChooser()
  3. getFileChooser()
  4. getFileChooser()
  5. getFileChooser(Component parent, JFileChooser fileChooser, JComponent accessory)
  6. getFileFromChooserForPNG()
  7. getFileFromChooserSave()
  8. getFileName(Component parent)
  9. getFileOrDir(String startName, boolean fileSelect)