How to restrict file choosers in java to specific files?
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
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.