How to restrict file choosers in java to specific

2019-04-04 14:00发布

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:
}

Hi guys....I'm 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.

4条回答
放我归山
2楼-- · 2019-04-04 14:11

This works fine in java8.1

JFileChooser dbfilechooser = new JFileChooser();
FileNameExtensionFilter filter1 = 
  new FileNameExtensionFilter("xls","xls");
FileNameExtensionFilter filter2 = 
  new FileNameExtensionFilter("xlsx", "xlsx");
FileNameExtensionFilter filter3 = 
  new FileNameExtensionFilter("csv", "csv");

dbfilechooser.addChoosableFileFilter(filter1);
dbfilechooser.addChoosableFileFilter(filter2);
dbfilechooser.addChoosableFileFilter(filter3);
查看更多
该账号已被封号
3楼-- · 2019-04-04 14:23

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.

查看更多
beautiful°
4楼-- · 2019-04-04 14:35

In my case, I had to put the

setFileFilter(          
    new FileNameExtensionFilter("Default file filter", ...)
);

after all other calls to the method

addChoosableFileFilter(...);

to make setAcceptAllFileFilterUsed(false) works.

查看更多
我想做一个坏孩纸
5楼-- · 2019-04-04 14:37

You can use FileNameExtensionFilter to add allowed extensions to your FileChooser dialog. Here's an example:

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
    "JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);

For more info, please refer to the Javadocs: FileNameExtensionFilter

查看更多
登录 后发表回答