Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Doubt regarding JFileChooser

  Asked By: Michael    Date: Oct 20    Category: Java    Views: 718

I want to add a Filter to the JFileChooser. The filter should list down
the folders and files with the extension .xml. The problem in writing custom
filter is it lists only the .xml files only. Folders are not visible. the code i
have typed is

jfc = new JFileChooser();
jfc.addChoosableFileFilter(new MyFilter());

MyFilter Class

public class MyFilter extends javax.swing.filechooser.FileFilter
public boolean accept(File file)
String filename = file.getName();
return filename.endsWith(".xml");
public String getDescription()
return "XML Files";

Can anyone help me to solve the problem? I didn't know how to list down xml
files along with the foldes.



1 Answer Found

Answer #1    Answered By: Alan Palmer     Answered On: Oct 20

Just add  a check to see if File.isDirectory(). If it is then return
true from the accept method.

Didn't find what you were looking for? Find more on Doubt regarding JFileChooser Or get search suggestion and latest updates.