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

Java 8Object Oriented ProgrammingProgramming

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

raja
Published on 08-May-2019 12:08:00
Advertisements