Java JFileChooser selectDirectory(Component component, String name, File pathToUse)

Here you can find the source of selectDirectory(Component component, String name, File pathToUse)

Description

select Directory

License

Open Source License

Declaration

public static String selectDirectory(Component component, String name, File pathToUse) 

Method Source Code


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

import java.awt.Component;

import java.io.*;

import javax.swing.JFileChooser;

public class Main {
    private static File lastChooserPath;
    private static File lastIndicatedPath;

    public static String selectDirectory(Component component, String name, File pathToUse) {

        JFileChooser chooser = new JFileChooser();
        boolean useIndicated = pathToUse != null;
        if (useIndicated && lastIndicatedPath == null) {
            lastIndicatedPath = pathToUse;
        }/*from  www  .j a v a 2s. c om*/

        chooser.setCurrentDirectory(useIndicated ? lastIndicatedPath : lastChooserPath);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int result = chooser.showDialog(component, name);
        if (result == JFileChooser.APPROVE_OPTION) {
            try {
                if (useIndicated) {
                    lastIndicatedPath = chooser.getCurrentDirectory();
                } else {
                    lastChooserPath = chooser.getCurrentDirectory();
                }
                return chooser.getSelectedFile().getAbsolutePath();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

Related

  1. saveObject(final Object content, final String description, final String extension, final File file)
  2. saveSystemFiles(Component owner)
  3. saveToFile(Object obj)
  4. select_file(Component parent, boolean show_save)
  5. selectAndSaveToFile(String content, Component parent)
  6. selectedFiles(JFileChooser chooser)
  7. selectFile(boolean exitOnCancel)
  8. selectFile(Component parent, boolean isOpen)
  9. selectFile(String msg, File dir, FileFilter filterOrNull)