Example usage for javax.swing.filechooser FileFilter FileFilter

List of usage examples for javax.swing.filechooser FileFilter FileFilter

Introduction

In this page you can find the example usage for javax.swing.filechooser FileFilter FileFilter.

Prototype

FileFilter

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {
    // Create a file filter to show only a directory or .doc files
    FileFilter filter = new FileFilter() {
        @Override/*from  www .  j av  a 2 s  . c o  m*/
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }

            String fileName = f.getName().toLowerCase();
            if (fileName.endsWith(".doc")) {
                return true;
            }

            return false; // Reject any other files
        }

        @Override
        public String getDescription() {
            return "Word Document";
        }
    };

    // Set the file filter
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileFilter(filter);

    int returnValue = fileChooser.showDialog(null, "Attach");
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        // Process the file
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String[] properties = { "os.name", "java.version", "java.vm.version", "java.vendor" };
    for (String property : properties) {
        System.out.println(property + ": " + System.getProperty(property));
    }/*ww  w .ja  v a 2 s  .c  om*/
    JFileChooser jfc = new JFileChooser();
    jfc.showOpenDialog(null);
    jfc.addChoosableFileFilter(new FileFilter() {
        @Override
        public boolean accept(File f) {
            return f.isDirectory() || f.getName().toLowerCase().endsWith(".obj");
        }

        @Override
        public String getDescription() {
            return "Wavefront OBJ (*.obj)";
        }

        @Override
        public String toString() {
            return getDescription();
        }
    });
    int result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    SwingUtilities.updateComponentTreeUI(jfc);
    jfc.showOpenDialog(null);
    result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");

    result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
    for (UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            UIManager.setLookAndFeel(info.getClassName());
            SwingUtilities.updateComponentTreeUI(jfc);
            break;
        }
    }
    jfc.showOpenDialog(null);
    result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
}

From source file:Main.java

public static File saveImageFile(Component parent) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileFilter(new FileFilter() {
        @Override/*from  w  w w. j  a v  a  2 s . com*/
        public String getDescription() {
            return "bmp";
        }

        @Override
        public boolean accept(File f) {
            String name = f.getName();
            boolean accepted = f.isDirectory() || name.endsWith(".bmp");
            return accepted;
        }
    });

    fileChooser.showSaveDialog(parent);
    return fileChooser.getSelectedFile();
}

From source file:Main.java

public static String showOpenFile(String currentDirectoryPath, Component parent, final String filterRegex,
        final String filterDescription) {
    JFileChooser fileChooser = new JFileChooser(currentDirectoryPath);
    fileChooser.addChoosableFileFilter(new FileFilter() {
        private Pattern regexPattern = Pattern.compile(filterRegex);

        public boolean accept(File f) {
            if (f.isDirectory())
                return true;
            return regexPattern.matcher(f.getName()).matches();
        }/*from  ww  w  .j  a  v a  2s  .c om*/

        public String getDescription() {
            return filterDescription;
        }

    });
    fileChooser.showOpenDialog(parent);

    File choosedFile = fileChooser.getSelectedFile();
    if (choosedFile == null)
        return null;
    return choosedFile.getAbsolutePath();
}

From source file:Main.java

/**
 * Dialog for choosing file./* w ww  .jav  a 2 s  .c  o  m*/
 * 
 * @param parent the parent component of the dialog, can be null
 * @return selected file or null if no file is selected
 */
public static File chooseImageFile(Component parent) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileFilter(new FileFilter() {
        @Override
        public String getDescription() {
            return "Supported image files(JPEG, PNG, BMP)";
        }

        @Override
        public boolean accept(File f) {
            String name = f.getName();
            boolean accepted = f.isDirectory() || name.endsWith(".jpg") || name.endsWith(".jpeg")
                    || name.endsWith(".jp2") || name.endsWith(".png") || name.endsWith(".bmp");
            return accepted;
        }
    });

    fileChooser.showOpenDialog(parent);
    return fileChooser.getSelectedFile();
}

From source file:Main.java

/**
 * Dialog for choosing file.//  w w  w  . j  av  a  2s  .c  o  m
 * 
 * @param parent the parent component of the dialog, can be null
 * @return selected file or null if no file is selected
 */
public static File chooseImageFile(Component parent) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileFilter(new FileFilter() {
        @Override
        public String getDescription() {
            return "Supported image files(JPEG, PNG, BMP)";
        }

        @Override
        public boolean accept(File f) {
            String name = f.getName();
            boolean accepted = f.isDirectory() || name.endsWith(".jpg") || name.endsWith(".jpeg")
                    || name.endsWith(".png") || name.endsWith(".bmp");
            return accepted;
        }
    });

    fileChooser.showOpenDialog(parent);
    return fileChooser.getSelectedFile();
}

From source file:Main.java

/**
 * Creates a FileFilter for a specified description
 * and an array of allowed extensions. <br />
 * /*  ww w  .  j  a v a 2  s.co  m*/
 * @param extensions the allowed extensions without a dot
 * @param description the displayed description
 * @return the created FileFilter
 */
public static FileFilter createFilter(final String[] extensions, final String description) {
    return new FileFilter() {

        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            }
            String name = file.getName().toLowerCase();
            for (String e : extensions) {
                if (name.endsWith("." + e.toLowerCase())) {
                    return true;
                }
            }
            return false;
        }

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

From source file:Main.java

/**
 * //from  www.  ja va  2  s .c  om
 * Example usage:
 * <pre>
 * SwingUtil.createFileFilter("JEQL script (*.jql)", "jql")
 * </pre>
 * @param description
 * @param extension
 * @return the file filter
 */
public static FileFilter createFileFilter(final String description, String extension) {
    final String dotExt = extension.startsWith(".") ? extension : "." + extension;
    FileFilter ff = new FileFilter() {
        public String getDescription() {
            return description;
        }

        public boolean accept(File f) {
            return f.isDirectory() || f.toString().toLowerCase().endsWith(dotExt);
        }
    };
    return ff;
}

From source file:Main.java

public static JFileChooser generateFileChooser(String description, String... ext) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fileChooser.setFileFilter(new FileFilter() {
        @Override/*  w w w .  ja  v  a  2s.co  m*/
        public boolean accept(File f) {
            boolean res = f.isDirectory();
            for (String s : ext) {
                res = res || f.getName().endsWith("." + s);
            }
            return res;
        }

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

    });
    return fileChooser;
}

From source file:kenh.xscript.ScriptUtils.java

public static void main(String[] args) {
    String file = null;// www  .ja va  2s.  c  om

    for (String arg : args) {
        if (StringUtils.startsWithAny(StringUtils.lowerCase(arg), "-f:", "-file:")) {
            file = StringUtils.substringAfter(arg, ":");
        }
    }

    Element e = null;
    try {
        if (StringUtils.isBlank(file)) {

            JFileChooser chooser = new JFileChooser();
            chooser.setDialogTitle("xScript");
            chooser.setAcceptAllFileFilterUsed(false);
            chooser.setFileFilter(new FileFilter() {
                public boolean accept(File f) {
                    if (f.isDirectory()) {
                        return true;
                    } else if (f.isFile() && StringUtils.endsWithIgnoreCase(f.getName(), ".xml")) {
                        return true;
                    }
                    return false;
                }

                public String getDescription() {
                    return "xScript (*.xml)";
                }
            });

            int returnVal = chooser.showOpenDialog(null);
            chooser.requestFocus();

            if (returnVal == JFileChooser.CANCEL_OPTION)
                return;

            File f = chooser.getSelectedFile();

            e = getInstance(f, null);
        } else {
            e = getInstance(new File(file), null);
        }
        //debug(e);
        //System.out.println("----------------------");

        int result = e.invoke();
        if (result == Element.EXCEPTION) {
            Object obj = e.getEnvironment().getVariable(Constant.VARIABLE_EXCEPTION);
            if (obj != null && obj instanceof Throwable) {
                System.err.println();
                ((Throwable) obj).printStackTrace();

            } else {
                System.err.println();
                System.err.println("Unknown EXCEPTION is thrown.");
            }
        }

    } catch (Exception ex) {
        ex.printStackTrace();

        System.err.println();

        if (ex instanceof UnsupportedScriptException) {
            UnsupportedScriptException ex_ = (UnsupportedScriptException) ex;
            if (ex_.getElement() != null) {
                debug(ex_.getElement(), System.err);
            }
        }
    } finally {
        if (e != null)
            e.getEnvironment().callback();
    }
}