Vipul Tyagi
Vipul Tyagi

Reputation: 667

Java FilenameExtensionFilter not working correctly

I am trying to make a utility in which user should be able to choose only ".pdf" files, nothing else. This is my code:

JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
       // jfc.setFileFilter(filter);
        jfc.addChoosableFileFilter(new FileNameExtensionFilter("*.pdf", "pdf"));

        int returnValue = jfc.showOpenDialog(null);
        // int returnValue = jfc.showSaveDialog(null);

        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File selectedFile = jfc.getSelectedFile();
            System.out.println(selectedFile.getAbsolutePath());
        }

But the problem is that file chooser dialog also gives an "All files" option, which is ruining the work. I want user to select only .pdf files. How this can be achieved?

Upvotes: 0

Views: 163

Answers (1)

Vipul Tyagi
Vipul Tyagi

Reputation: 667

If anyone is facing a problem like me, there is only one ray of hope. Along with FileNameExtensionFilter, we have to use JFileChooser.setAcceptAllFileFilterUsed(false);.

Upvotes: 1

Related Questions