Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict file choosers in java to specific files?

private void openMenuActionPerformed(java.awt.event.ActionEvent evt) {
    
    DBmanager db = new DBmanager();
    if (!db.getCurrentUser().equals("Admin")) {
        JOptionPane.showMessageDialog(this, "You are Not Allowed to Run Applications");
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PDF Documents", "pdf"));
        fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("MS Office Documents", "docx", "xlsx", "pptx"));
        fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Images", "jpg", "png", "gif", "bmp"));
        fileChooser.setAcceptAllFileFilterUsed(false);
        int returnVal = fileChooser.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();

            if (Desktop.isDesktopSupported()) {
                try {
                    Desktop.getDesktop().open(file);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    } else if (db.getCurrentUser().equals("Admin")) {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setAcceptAllFileFilterUsed(true);
        int returnVal = fileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            if (Desktop.isDesktopSupported()) {
                try {
                    Desktop.getDesktop().open(file);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }// TODO add your handling code here:
}

I am trying to filter files in a file filter by setting fileChooser.setAcceptAllFileFilterUsed(false);. The "all files" option disappears from the FileChooser but all files remain visible unless you select an option from PDF documents,ms Office or images. I want to have only my 3 custom filters upon opening the file chooser.

like image 300
Julius Gitonga Avatar asked Sep 02 '13 14:09

Julius Gitonga


People also ask

How do I restrict a file type in JFileChooser?

For example, if you want to filter your JFileChooser to strictly display most commonly found image files, you would use something like this: FileNameExtensionFilter filter = new FileNameExtensionFilter("Image Files", "jpg", "png", "gif", "jpeg"); JFileChooser fileChooser = new JFileChooser(); fileChooser.

Can file Choosers permit selecting directories?

To get the chosen file (or directory, if you set up the file chooser to allow directory selections), call the getSelectedFile method on the file chooser. This method returns an instance of File . The example obtains the name of the file and uses it in the log message.

What is file chooser Java?

JFileChooser is a part of java Swing package. The java Swing package is part of JavaTM Foundation Classes(JFC) . JFC contains many features that help in building graphical user interface in java . Java Swing provides components such as buttons, panels, dialogs, etc .


1 Answers

For example, if you want to filter your JFileChooser to strictly display most commonly found image files, you would use something like this:

FileNameExtensionFilter filter = new FileNameExtensionFilter("Image Files", "jpg", "png", "gif", "jpeg");
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(filter);

The first argument is the description (what gets displayed upon selection at the bottom) and the second argument are the informal file extensions.

like image 155
Josh M Avatar answered Oct 16 '22 06:10

Josh M