Here you can find the source of selectFileForOpen(final Component parent, final FileFilter[] fileFilters, final String title, final FileFilter[] selectedFilters, final File initFile)
public static File selectFileForOpen(final Component parent, final FileFilter[] fileFilters, final String title, final FileFilter[] selectedFilters, final File initFile)
//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; } } }