by Dinesh Thakur

 

import java.io.*;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JavaExampleFilterFileChooserInSwing extends JFrame implements ActionListener
{
    JFileChooser Flchsr = new JFileChooser();
    JButton BtnFilechsr = new JButton("Show File Chooser");
    JTextField Txt = new JTextField(20);
    public JavaExampleFilterFileChooserInSwing()
     {
        super("Example of Filter File Chooser in Java Swing");
        Container Cntnr = getContentPane();
        Cntnr.setLayout(new FlowLayout());
        Cntnr.add(BtnFilechsr);       
        Cntnr.add(Txt);       
        BtnFilechsr.addActionListener(this);
        Flchsr.addChoosableFileFilter(new FiltrOne());
        Flchsr.addChoosableFileFilter(new FiltrTwo());
     }
           public void actionPerformed(ActionEvent e1)
           {
                int Rsult = Flchsr.showOpenDialog(null);
                if(Rsult == JFileChooser.APPROVE_OPTION)
               {
                      Txt.setText("You Choose" +Flchsr.getSelectedFile().getPath());
                   }
           }
                     public static void main(String aa[])
                     {
                         JFrame Frm = new JavaExampleFilterFileChooserInSwing();
                         Frm.setBounds(210,210,310,310);
                         Frm.setVisible(true);
                         Frm.setDefaultCloseOperation(
                        WindowConstants.DISPOSE_ON_CLOSE);
                        Frm.addWindowListener(new WindowAdapter()
                       {
                              public void windowClosing(WindowEvent e2)
                              {
                                 System.exit(0);
                              }
                          });
                     }
 }
                         class FiltrOne extends javax.swing.filechooser.FileFilter
                         {
                             public boolean accept(File Fileob1)
                              {
                                 String Extnsn = " ";
                                 if(Fileob1.getPath().lastIndexOf('.') > 0)
                                 Extnsn = Fileob1.getPath().substring(Fileob1.getPath().lastIndexOf('.')+ 1).toLowerCase();
                                 if(Extnsn != "")
                                 return Extnsn.equals("gif");
                                 else
                                 return Fileob1.isDirectory();
                              }
                                 public String getDescription()
                                  {
                                     return "Gif Files (*.gif)";
                                  }
                        }
                                class FiltrTwo extends javax.swing.filechooser.FileFilter
                                   {
                                        public boolean accept(File Fileob1)
                                         {
                                             String Extnsn = "";
                                             if(Fileob1.getPath().lastIndexOf('.') > 0)
                                             Extnsn = Fileob1.getPath().substring(Fileob1.getPath().lastIndexOf('.')+ 1).toLowerCase();
                                             if(Extnsn != "")
                                             return Extnsn.equals("java");
                                             else
                                             return Fileob1.isDirectory();
                                         }
                                             public String getDescription()
                                                {
                                                      return "Java Files (*.java)";
                                                }
                                 }
FileFilter Java Swing Example