Java JFileChooser ChooseFile(final String desc, final String[] allowed_extensions, String suggested_dir)

Here you can find the source of ChooseFile(final String desc, final String[] allowed_extensions, String suggested_dir)

Description

Choose File

License

Open Source License

Declaration

public static String ChooseFile(final String desc, final String[] allowed_extensions, String suggested_dir) 

Method Source Code

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

import javax.swing.JFileChooser;

public class Main {
    private static String _last_dir = "";

    public static String ChooseFile(final String desc, final String allowed_extension) {
        return ChooseFile(desc, new String[] { allowed_extension }, _last_dir);
    }//from  w w w .  j a va 2  s  .c o m

    public static String ChooseFile(final String desc, final String allowed_extension, String suggested_dir) {
        return ChooseFile(desc, new String[] { allowed_extension }, suggested_dir);
    }

    public static String ChooseFile(final String desc, final String[] allowed_extensions) {
        return ChooseFile(desc, allowed_extensions, _last_dir);
    }

    public static String ChooseFile(final String desc, final String[] allowed_extensions, String suggested_dir) {
        String basedir = suggested_dir.isEmpty() ? _last_dir : suggested_dir;
        JFileChooser chooser = _NewFileChooserInstance(basedir);
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        if (!desc.isEmpty() && allowed_extensions.length > 0)
            chooser.setFileFilter(newFileFilter(desc, allowed_extensions));
        int returnVal = chooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            _last_dir = chooser.getSelectedFile().getAbsolutePath();
            return chooser.getSelectedFile().getAbsolutePath();
        } else {
            return "";
        }
    }

    private static JFileChooser _NewFileChooserInstance() {
        if (_last_dir.isEmpty())
            _last_dir = System.getProperty("user.dir");
        return new JFileChooser(_last_dir);
    }

    private static JFileChooser _NewFileChooserInstance(String suggested_dir) {
        _last_dir = suggested_dir;
        return new JFileChooser(suggested_dir);
    }

    private static javax.swing.filechooser.FileFilter newFileFilter(final String desc,
            final String[] allowed_extensions) {
        return new javax.swing.filechooser.FileFilter() {
            @Override
            public boolean accept(java.io.File f) {
                if (f.isDirectory()) {
                    return true;
                }
                int pos = f.getName().lastIndexOf('.');
                if (pos == -1) {
                    return false;
                } else {
                    String extension = f.getName().substring(pos + 1);
                    for (String allowed_extension : allowed_extensions) {
                        if (extension.equalsIgnoreCase(allowed_extension)) {
                            return true;
                        }
                    }
                    return false;
                }
            }

            @Override
            public String getDescription() {
                return desc;
            }
        };
    }
}

Related

  1. chooseFile(Component p, String message, boolean toOpen, int fileType)
  2. chooseFile(Component parent, String title, boolean open)
  3. chooseFile(Component parentComponent, String title, File curDir, String suffix)
  4. chooseFile(File initialFile, boolean load)
  5. chooseFile(final Component parent, final boolean filesOnly, final String title, final File selectedFile, final FileFilter filter)
  6. chooseFile(JFileChooser fc, Component parent)
  7. chooseFile(String title)
  8. chooseFiles()
  9. chooseFiles(final Component parent)