Disable "All files" when using FileNameExtensionFilter in java

user2970210 picture user2970210 · Mar 28, 2014 · Viewed 10.6k times · Source

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?

Answer

MadProgrammer picture MadProgrammer · Mar 28, 2014

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.