Java JFileChooser getOpenFile(String defaultPath)

Here you can find the source of getOpenFile(String defaultPath)

Description

Generic method to get a file using a JFileChooser

License

Open Source License

Parameter

Parameter Description
defaultPath a parameter

Return

the File or null if aborted.

Declaration

public static File getOpenFile(String defaultPath) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import javax.swing.JFileChooser;

public class Main {
    /**// w w w  .  ja v a2s. c  o m
     * Generic method to get a file using a JFileChooser
     * 
     * @param defaultPath
     * @return the File or null if aborted.
     */
    public static File getOpenFile(String defaultPath) {
        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) {
            // Save the selected path for next time
            defaultPath = chooser.getSelectedFile().getParentFile().getPath();
            // Process the file
            file = chooser.getSelectedFile();
        }
        return file;
    }
}

Related

  1. getIcon(File file)
  2. getJFileChooser(String title, File initialDirectory, File initialFile, final FilenameFilter filter, int directoryMode)
  3. getLoadFilename(Component parent)
  4. getLoadFiles(String message, File openDefaultDirectory, String description, String... extensions)
  5. getNewFileChooser()
  6. getOpenFile(String name, String currentDirectory)
  7. getOpenFile(String title)
  8. getPropertiesFile(boolean saving, String startName, String extension, String description)
  9. getSaveAsFile(String defaultName, String currentDirectory, String defaultExtension)