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

JFileChoosercreateUserDefinedProfileFileChooser()
Creates a JFileChooser which is appropriate for opening multiple files containing user defined profiles.
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.setMultiSelectionEnabled(true);
fileChooser.setFileFilter(new FileFilter() {
    public boolean accept(File file) {
        String s = file.getName().toLowerCase();
        return file.isDirectory()
                || (file.isFile() && (s.endsWith(".xmi") || s.endsWith(".xml") || s.endsWith(".uml") 
...
File[]directoryFetch(File starting)
Creates a JFileChooser that allows user to select multiple directories.
if (!starting.isDirectory()) 
    throw new IllegalArgumentException("Specified directory does not exist or is not a directory");
JFileChooser fc = new JFileChooser(starting);
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setDialogTitle("select directories");
fc.setMultiSelectionEnabled(true);
if (fc.showOpenDialog(null) != JFileChooser.APPROVE_OPTION)
    System.exit(0);
...
voidexportFile(JFileChooser fileChooser, String contents)
export File
int result = fileChooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
    try {
        FileWriter fstream = new FileWriter(fileChooser.getSelectedFile().toString());
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(contents);
        out.close();
    } catch (Exception e) {
...
JFileChooserfileChooser()
file Chooser
JFileChooser fc = new JFileChooser();
fc.setAcceptAllFileFilterUsed(false);
fc.setCurrentDirectory(new java.io.File("."));
return fc;
FilegetChoiceFileFromUser()
get Choice File From User
fc.setMultiSelectionEnabled(false);
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int exitStatus = fc.showOpenDialog(null);
if (exitStatus == JFileChooser.APPROVE_OPTION) {
    return fc.getSelectedFile();
} else {
    return null;
FilegetDir(String title, String initDir, boolean allowFiles, Component parent)
Prompt the user to select a directory (or file).
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle(title);
fileChooser.setFileSelectionMode(
        allowFiles ? JFileChooser.FILES_AND_DIRECTORIES : JFileChooser.DIRECTORIES_ONLY);
fileChooser.setMultiSelectionEnabled(false);
if (initDir != null && !initDir.isEmpty()) {
    File initDirFile = new File(initDir);
    if (initDirFile.isDirectory() || (allowFiles && initDirFile.isFile())) {
...
StringgetDriveDisplayName(File path)
get Drive Display Name
FileSystemView fsv = new javax.swing.JFileChooser().getFileSystemView();
if (fsv != null) {
    File tmp = path;
    while (tmp.getParentFile() != null)
        tmp = tmp.getParentFile();
    String displayName = fsv.getSystemDisplayName(tmp);
    if (!displayName.trim().equals(""))
        return displayName;
...
StringgetExtension(File f)

Method to get the extension of the file, in lowercase.

String s = f.getName();
int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1)
    return s.substring(i + 1).toLowerCase();
return "";
FilegetFile()
get File
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
    return fileChooser.getSelectedFile();
return null;
FilegetFile(Component parent, String title, boolean write)
Shows a dialog to open/save file (the write parameter steers this).
JFileChooser fc = new JFileChooser();
fc.setDialogTitle(title);
if (write) {
    fc.showSaveDialog(parent);
} else {
    fc.showOpenDialog(parent);
File f = fc.getSelectedFile();
...