Java JFileChooser getOpenFile(String name, String currentDirectory)

Here you can find the source of getOpenFile(String name, String currentDirectory)

Description

get Open File

License

Apache License

Declaration

public static File getOpenFile(String name, String currentDirectory) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.swing.*;

import java.io.*;

public class Main {
    protected static String gLastDirectory = System.getProperty("user.dir");

    public static File getOpenFile(String name, String currentDirectory) {
        return getOpenFile(name, currentDirectory, null);

    }//  w ww. ja  v  a2  s  . co  m

    public static File getOpenFile(String name, String currentDirectory, String[] extension) {
        if (currentDirectory == null)
            currentDirectory = gLastDirectory;
        JFileChooser fc = new JFileChooser(currentDirectory);
        File directory = new File(currentDirectory);
        File theFile = new File(directory, name);
        fc.setCurrentDirectory(directory);
        fc.setSelectedFile(theFile);
        fc.setDialogType(JFileChooser.OPEN_DIALOG);
        int ret = fc.showSaveDialog(null);
        if (ret == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fc.getSelectedFile();
            gLastDirectory = selectedFile.getParent();
            return selectedFile;
        } else {
            return null;
        }
    }
}

Related

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