Java JFileChooser selectFileForOpen(final Component parent, final FileFilter[] fileFilters, final String title, final FileFilter[] selectedFilters, final File initFile)

Here you can find the source of selectFileForOpen(final Component parent, final FileFilter[] fileFilters, final String title, final FileFilter[] selectedFilters, final File initFile)

Description

select File For Open

License

Apache License

Declaration

public static File selectFileForOpen(final Component parent, final FileFilter[] fileFilters, final String title,
            final FileFilter[] selectedFilters, final File initFile) 

Method Source Code


//package com.java2s;
/* //  www.j  a v  a 2 s  .  com
 * Copyright 2015 Igor Maznitsa (http://www.igormaznitsa.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.awt.Component;

import java.io.File;

import javax.swing.JFileChooser;

import javax.swing.filechooser.FileFilter;

public class Main {
    public static File selectFileForOpen(final Component parent, final FileFilter[] fileFilters, final String title,
            final FileFilter[] selectedFilters, final File initFile) {
        JFileChooser chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(false);
        chooser.setDragEnabled(false);
        chooser.setControlButtonsAreShown(true);
        chooser.setDialogType(JFileChooser.OPEN_DIALOG);
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

        for (final FileFilter fileFilter : fileFilters) {
            chooser.addChoosableFileFilter(fileFilter);
        }

        chooser.setDialogTitle(title);
        chooser.setAcceptAllFileFilterUsed(false);

        if (initFile != null) {
            chooser.setCurrentDirectory(initFile);
            chooser.setName(initFile.getName());
        }

        int returnVal = chooser.showDialog(parent, "Open");
        selectedFilters[0] = chooser.getFileFilter();

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File p_file = chooser.getSelectedFile();
            return p_file;
        } else {
            return null;
        }

    }
}

Related

  1. selectDirectory(Component component, String name, File pathToUse)
  2. selectedFiles(JFileChooser chooser)
  3. selectFile(boolean exitOnCancel)
  4. selectFile(Component parent, boolean isOpen)
  5. selectFile(String msg, File dir, FileFilter filterOrNull)
  6. selectNonExistingFile(Component parent, String extensionWanted)
  7. setCurrentDirectory(JFileChooser fileChooser, String location)
  8. setFile(File file, JFileChooser chooser)
  9. setFileChooserFilters(JFileChooser fileDlg, String filters, int selectIndex)