Example usage for javax.swing JFileChooser addChoosableFileFilter

List of usage examples for javax.swing JFileChooser addChoosableFileFilter

Introduction

In this page you can find the example usage for javax.swing JFileChooser addChoosableFileFilter.

Prototype

@BeanProperty(preferred = true, description = "Adds a filter to the list of user choosable file filters.")
public void addChoosableFileFilter(FileFilter filter) 

Source Link

Document

Adds a filter to the list of user choosable file filters.

Usage

From source file:Main.java

public static void main(String[] args) {
    FileNameExtensionFilter extFilter = new FileNameExtensionFilter("Java Source  File", "java", "jav");
    // Set the file filter
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.addChoosableFileFilter(extFilter);

    int returnValue = fileChooser.showDialog(null, "Attach");
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        // Process the file
    }/*  w w w.  j  a v  a 2 s .c  om*/

}

From source file:Main.java

public static void main(String[] argv) {
    JFileChooser fileChooser = new JFileChooser(new File("."));
    fileChooser.addChoosableFileFilter(new MyFilter());

    fileChooser.showOpenDialog(null);/* w  ww . jav a  2  s  . c  o  m*/
    System.out.println(fileChooser.getSelectedFile());

}

From source file:Main.java

public static void main(String[] args) {
    final JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton loadButton = new JButton("Display Image");
    loadButton.addActionListener(ev -> {
        JFileChooser fc = new JFileChooser(System.getProperty("user.home"));
        fc.addChoosableFileFilter(
                new FileNameExtensionFilter("Image files", new String[] { "png", "jpg", "jpeg", "gif" }));
        if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            try {
                Image image = ImageIO.read(fc.getSelectedFile());
                if (image != null) {
                    JPanel panel = new JPanel(new BorderLayout(10, 10));
                    panel.add(new JLabel(fc.getSelectedFile().toString()), BorderLayout.NORTH);
                    panel.add(new JLabel(new ImageIcon(image)));
                    JOptionPane.showMessageDialog(frame, panel);
                }//w  w  w.j  a v  a  2s . c om
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });

    frame.add(loadButton);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
}

From source file:FileChooserDialog.java

public static void main(String[] args) {
    JFileChooser fileopen = new JFileChooser();
    FileFilter filter = new FileNameExtensionFilter("c files", "c");
    fileopen.addChoosableFileFilter(filter);

    int ret = fileopen.showDialog(null, "Open file");

    if (ret == JFileChooser.APPROVE_OPTION) {
        File file = fileopen.getSelectedFile();
        System.out.println(file);
    }/*  w  w w.  j  a  v a2s  .  c  o  m*/
}

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));
    }//from w  w w . j  a v  a2s .  c o  m
    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:ExtensionFileFilter.java

public static void main(String[] a) {
    JFileChooser fileChooser = new JFileChooser(".");
    FileFilter jpegFilter = new ExtensionFileFilter(null, new String[] { "JPG", "JPEG" });

    fileChooser.addChoosableFileFilter(jpegFilter);

    fileChooser.showOpenDialog(null);//w  w  w  .java2 s . c om
}

From source file:Main.java

public static void main(String[] args) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setAcceptAllFileFilterUsed(false);
    FileNameExtensionFilter filter = new FileNameExtensionFilter("MPEG3 songs", "mp3");
    fileChooser.addChoosableFileFilter(filter);
    fileChooser.showOpenDialog(null);/* w w w . ja v  a 2 s. co  m*/

}

From source file:FilterSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JFileChooser Filter Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    final JLabel directoryLabel = new JLabel();
    contentPane.add(directoryLabel, BorderLayout.NORTH);

    final JLabel filenameLabel = new JLabel();
    contentPane.add(filenameLabel, BorderLayout.SOUTH);

    final JButton button = new JButton("Open FileChooser");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component parent = (Component) actionEvent.getSource();
            JFileChooser fileChooser = new JFileChooser(".");
            fileChooser.setAccessory(new LabelAccessory(fileChooser));
            FileFilter filter1 = new ExtensionFileFilter(null, new String[] { "JPG", "JPEG" });
            //        fileChooser.setFileFilter(filter1);
            fileChooser.addChoosableFileFilter(filter1);
            FileFilter filter2 = new ExtensionFileFilter("gif", new String[] { "gif" });
            fileChooser.addChoosableFileFilter(filter2);
            fileChooser.setFileView(new JavaFileView());
            int status = fileChooser.showOpenDialog(parent);
            if (status == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                directoryLabel.setText(selectedFile.getParent());
                filenameLabel.setText(selectedFile.getName());
            } else if (status == JFileChooser.CANCEL_OPTION) {
                directoryLabel.setText(" ");
                filenameLabel.setText(" ");
            }/* www.  j a  v a 2s.  c  o  m*/
        }
    };
    button.addActionListener(actionListener);
    contentPane.add(button, BorderLayout.CENTER);

    frame.setSize(300, 200);
    frame.setVisible(true);
}

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  w w  w  .j a  v  a 2 s .  com*/

        public String getDescription() {
            return filterDescription;
        }

    });
    fileChooser.showOpenDialog(parent);

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

From source file:ExcelComponents.FileOpener.java

public static File openfile() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setAcceptAllFileFilterUsed(false);
    fileChooser.addChoosableFileFilter(excelfilter);

    int returnValue = fileChooser.showOpenDialog(null);
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        return (selectedFile);
    }//from w  ww  . ja v a 2 s  . c  o  m
    return null;

}