Java Utililty Methods JFileChooser

List of utility methods to do JFileChooser

Description

The list of methods to do JFileChooser are organized into topic(s).

Method

File[]getFilesToOpen(File startDirectory, Component parent, String... filters)
get Files To Open
JFileChooser chooser = new JFileChooser(startDirectory);
FileNameExtensionFilter filter = new FileNameExtensionFilter("Soubory " + toString(filters), filters);
chooser.setFileFilter(filter);
chooser.setMultiSelectionEnabled(true);
int returnValue = chooser.showOpenDialog(parent);
if (returnValue == JFileChooser.APPROVE_OPTION) {
    return chooser.getSelectedFiles();
return new File[0];
FilegetFileToSave(String description, String extension, Component component)
get File To Save
JFileChooser fileChooser = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter(description, new String[] { extension });
fileChooser.setFileFilter(filter);
int action = fileChooser.showSaveDialog(component);
if (action == JFileChooser.APPROVE_OPTION) {
    if (fileChooser.getSelectedFile() != null) {
        File theFile = fileChooser.getSelectedFile();
        if (!theFile.getAbsolutePath().toLowerCase().endsWith("." + extension)) {
...
FilegetFileWithExtension(JFileChooser c)
Returns the selected file from a JFileChooser, including the extension from the file filter.
File file = c.getSelectedFile();
if (c.getFileFilter() instanceof FileNameExtensionFilter) {
    String[] exts = ((FileNameExtensionFilter) c.getFileFilter()).getExtensions();
    String nameLower = file.getName().toLowerCase();
    for (String ext : exts) { 
        if (nameLower.endsWith('.' + ext.toLowerCase())) {
            return file; 
    file = new File(file.toString() + '.' + exts[0]);
return file;
JFileChoosergetFixedFileChooser(File file)

Returns a file chooser that should not trip the bug with Java and Windows XP zip/rar folder feature.

JFileChooser fileChooser = file != null ? new JFileChooser(file) : new JFileChooser();
fileChooser.putClientProperty("FileChooser.useShellFolder", Boolean.FALSE);
return fileChooser;
IcongetIcon(File file)
get Icon
if (file == null) {
    return null;
return basicChooser.getIcon(file);
JFileChoosergetJFileChooser(String title, File initialDirectory, File initialFile, final FilenameFilter filter, int directoryMode)
get J File Chooser
JFileChooser fileChooser = new JFileChooser();
if (initialDirectory != null) {
    fileChooser.setCurrentDirectory(initialDirectory);
if (initialFile != null) {
    fileChooser.setSelectedFile(initialFile);
if (filter != null) {
...
FilegetLoadFilename(Component parent)
Get a filename to load from.
_fileChooser.setMultiSelectionEnabled(false);
_fileChooser.setSelectedFile(new File("x")); 
_fileChooser.setSelectedFile(new File(""));
int option = _fileChooser.showOpenDialog(parent);
if (option == JFileChooser.APPROVE_OPTION) {
    File filename = _fileChooser.getSelectedFile();
    if (filename.exists()) {
        return filename;
...
File[]getLoadFiles(String message, File openDefaultDirectory, String description, String... extensions)
get Load Files
java.awt.Frame frame = new java.awt.Frame();
frame.setTitle(message);
final JFileChooser chooser = new JFileChooser(openDefaultDirectory);
chooser.setMultiSelectionEnabled(true);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
FileNameExtensionFilter filter = new FileNameExtensionFilter(description, extensions);
chooser.setFileFilter(filter);
final int returnVal = chooser.showOpenDialog(frame);
...
JFileChoosergetNewFileChooser()
get New File Chooser
return getNewFileChooser(FILE_FILTER_EXCEL, FILE_FILTER_CSV);
FilegetOpenFile(String defaultPath)
Generic method to get a file using a JFileChooser
File file = null;
JFileChooser chooser = new JFileChooser();
if (defaultPath != null) {
    chooser.setCurrentDirectory(new File(defaultPath));
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
    defaultPath = chooser.getSelectedFile().getParentFile().getPath();
...