How to create FileFilter for JFileChooser in Java and display File Type accordingly?


To create FileFilter, use the FileNamExtensionFilter class. The following is an example to display File Type in JFileChooser −

Example

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class SwingDemo {
   public static void main(String[] args) {
      JFileChooser file = new JFileChooser();
      file.setAcceptAllFileFilterUsed(false);
      FileNameExtensionFilter extFilter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
      file.addChoosableFileFilter(extFilter);
      file.showOpenDialog(null);
   }
}

Output

Updated on: 30-Jul-2019

603 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements