Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable "All files" when using FileNameExtensionFilter in java

I am using the FileNameExtensionFilter to filter the file type but when the user selects the open button, they have the option to select "All files" from the file type. is there a way to disable that option?

 FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
 JFileChooser fileChooser = ...;
 fileChooser.addChoosableFileFilter(filter);

so something to add to above code which disallow user to select any other file?

like image 408
user2970210 Avatar asked Mar 28 '14 05:03

user2970210


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.

What is FileNameExtensionFilter in Java?

public final class FileNameExtensionFilter extends FileFilter. An implementation of FileFilter that filters using a specified set of extensions. The extension for a file is the portion of the file name after the last ".". Files whose name does not contain a "." have no file name extension.


Video Answer


1 Answers

Take a look at JFileChooser#setAcceptAllFileFilterUsed

Determines whether the AcceptAll FileFilter is used as an available choice in the choosable filter list. If false, the AcceptAll file filter is removed from the list of available file filters. If true, the AcceptAll file filter will become the the actively used file filter.

like image 164
MadProgrammer Avatar answered Oct 07 '22 11:10

MadProgrammer