Java JFileChooser createFileChooser(String title, File dir, String filter)

Here you can find the source of createFileChooser(String title, File dir, String filter)

Description

create File Chooser

License

Open Source License

Declaration

public static JFileChooser createFileChooser(String title, File dir,
            String filter) 

Method Source Code

//package com.java2s;
/*//from ww w. ja v a  2s  .c  o m
 * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
 * All rights reserved. This program is made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 * Contributors:
 * General Robotix Inc.
 * National Institute of Advanced Industrial Science and Technology (AIST) 
 */

import java.io.File;

import javax.swing.JFileChooser;

import javax.swing.filechooser.FileFilter;

public class Main {
    public static JFileChooser createFileChooser(String title, File dir,
            String filter) {
        return createFileChooser(title, dir, new String[] { filter });
    }

    public static JFileChooser createFileChooser(String title, File dir,
            String[] filter) {
        JFileChooser jFileChooser = new JFileChooser();
        // title
        if (title != null)
            jFileChooser.setDialogTitle(title);
        // directory
        if (dir == null)
            dir = new File(System.getProperty("user.dir"));
        jFileChooser.setCurrentDirectory(dir);
        // filter
        FileFilter[] ff = jFileChooser.getChoosableFileFilters();
        for (int i = 1; i < ff.length; i++)
            jFileChooser.removeChoosableFileFilter(ff[i]);
        for (int i = 0; i < filter.length; i++) {
            if (filter[i] != null && !filter[i].equals(""))
                jFileChooser
                        .addChoosableFileFilter(createFileFilter(filter[i]));
        }
        return jFileChooser;
    }

    public static FileFilter createFileFilter(final String filter) {
        return new FileFilter() {
            public boolean accept(File f) {
                String ext = "";
                String path = f.getPath();
                int idx = path.lastIndexOf('.');
                if (idx > 0) {
                    ext = path.substring(idx + 1).toLowerCase();
                    if (ext.equals(filter))
                        return true;
                }
                return f.isDirectory();
            }

            public String getDescription() {
                return filter + " files (*." + filter + ")";
            }
        };
    }
}

Related

  1. configureFileChooser(JFileChooser fchooser, boolean directoryOnly, File selectedFile)
  2. correctSelectedFileExtension(JFileChooser chooser)
  3. createChooser(String lastPath, final boolean checkOverrideFile)
  4. createFileChooser(File presetFile, final String... postfixes)
  5. createFileChooser(String initDir)
  6. createJFileChooser(String name)
  7. createJFileChooserWithExistenceChecking()
  8. createJFileChooserWithOverwritePrompting()
  9. createOpenFileChooser(String title, String dir, Component parent, FileFilter filter)