Java tutorial
//package com.java2s; import java.io.File; import javax.swing.JFileChooser; public class Main { /** * Displays a {@link JFileChooser} to select a directory. * * @param title The title of the dialog. * @param startDirectory The directory where the dialog is initialed opened. * @return The {@link File} selected, returns null if no directory was selected. */ public static File chooseDirectory(String title, String startDirectory) { JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setDialogTitle(title); if (startDirectory != null && !startDirectory.trim().equals("")) { chooser.setCurrentDirectory(new File(startDirectory)); } int status = chooser.showOpenDialog(null); if (status == JFileChooser.APPROVE_OPTION) { return chooser.getSelectedFile(); } return null; } /** * Displays a {@link JFileChooser} to select a directory. * * @param title The title of the dialog. * @param startDirectory The directory where the dialog is initialed opened. * @return The {@link File} selected, returns null if no directory was selected. */ public static File chooseDirectory(String title, File startDirectory) { JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setDialogTitle(title); if (startDirectory != null) { chooser.setCurrentDirectory(startDirectory); } int status = chooser.showOpenDialog(null); if (status == JFileChooser.APPROVE_OPTION) { return chooser.getSelectedFile(); } return null; } /** * Displays a {@link JFileChooser} to select a directory. * * @param title The title of the dialog. * @return The {@link File} selected, returns null if no directory was selected. */ public static File chooseDirectory(String title) { return chooseDirectory(title, ""); } }