Java JFileChooser getJFileChooser(String title, File initialDirectory, File initialFile, final FilenameFilter filter, int directoryMode)

Here you can find the source of getJFileChooser(String title, File initialDirectory, File initialFile, final FilenameFilter filter, int directoryMode)

Description

get J File Chooser

License

LGPL

Parameter

Parameter Description
title a parameter
initialDirectory a parameter
initialFile a parameter
filter a parameter
directoryMode either JFileChooser.DIRECTORIES_ONLY, JFileChooser.FILES_ONLY, or JFileChooser.DIRECTORIES_ONLY : JFileChooser.FILES_AND_DIRECTORIES

Declaration

private static JFileChooser getJFileChooser(String title, File initialDirectory, File initialFile,
        final FilenameFilter filter, int directoryMode) 

Method Source Code

//package com.java2s;
/*/*from ww w.java 2 s .  co  m*/
 * Copyright (c) 2007-2013 The Broad Institute, Inc.
 * SOFTWARE COPYRIGHT NOTICE
 * This software and its documentation are the copyright of the Broad Institute, Inc. All rights are reserved.
 *
 * This software is supplied without any warranty or guaranteed support whatsoever. The Broad Institute is not responsible for its use, misuse, or functionality.
 *
 * This software is licensed under the terms of the GNU Lesser General Public License (LGPL),
 * Version 2.1 which is available at http://www.opensource.org/licenses/lgpl-2.1.php.
 */

import javax.swing.*;
import javax.swing.filechooser.FileFilter;

import java.io.File;
import java.io.FilenameFilter;

public class Main {
    /**
     * @param title
     * @param initialDirectory
     * @param initialFile
     * @param filter
     * @param directoryMode    either JFileChooser.DIRECTORIES_ONLY, JFileChooser.FILES_ONLY, or
     *                         JFileChooser.DIRECTORIES_ONLY : JFileChooser.FILES_AND_DIRECTORIES
     * @return
     */
    private static JFileChooser getJFileChooser(String title, File initialDirectory, File initialFile,
            final FilenameFilter filter, int directoryMode) {
        JFileChooser fileChooser = new JFileChooser();
        if (initialDirectory != null) {
            fileChooser.setCurrentDirectory(initialDirectory);
        }
        if (initialFile != null) {
            fileChooser.setSelectedFile(initialFile);
        }
        if (filter != null) {
            fileChooser.setFileFilter(new FileFilter() {
                @Override
                public boolean accept(File file) {
                    return filter.accept(file.getParentFile(), file.getName());
                }

                @Override
                public String getDescription() {
                    return "";
                }
            });
        }

        fileChooser.setDialogTitle(title);
        fileChooser.setFileSelectionMode(directoryMode);

        return fileChooser;
    }
}

Related

  1. getFilesToOpen(File startDirectory, Component parent, String... filters)
  2. getFileToSave(String description, String extension, Component component)
  3. getFileWithExtension(JFileChooser c)
  4. getFixedFileChooser(File file)
  5. getIcon(File file)
  6. getLoadFilename(Component parent)
  7. getLoadFiles(String message, File openDefaultDirectory, String description, String... extensions)
  8. getNewFileChooser()
  9. getOpenFile(String defaultPath)