• Post Reply Bookmark Topic Watch Topic
  • New Topic

JFileChooser Filter Problem  RSS feed

 
singh sagar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
I need to use a file chooser for open button which shows only text files and Folders(directories)

I have used this code for my file chooser filter,but its showing only text files not directories,

class TextFilter implements FileFilter
{

public boolean accept(File f)
{
if (f.isDirectory())
return true;
String s = f.getName();
int i = s.lastIndexOf('.');

if (i > 0 && i < s.length() - 1)
if (s.substring(i + 1).toLowerCase().equals("txt"))
return true;

return false;
}

public String getDescription()
{
return "Accepts txt files only.";
}
}

please help.
Thanks
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which FileFilter are you using? Is it javax.swing.filechooser.FileFilter?

It should be public class TextFilter extends FileFilter.

And you can greatly simplify your accept method to
 
singh sagar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Campbell Ritchie
thanks for the reply
but when i changed implements to extends it gave error:
"no interface expected here"
 
singh sagar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after using javax.swing.filechooser.FileFilter its not giving error now but still there is no change in output
only txt files are visible
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What files and directories do you have in your current directory (using the setCurrentDirectory and getCurrentDirectory methods)?
 
singh sagar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its my My Documents Folder
with two folders and two text files
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am surprised you are not finding directories. Please confirm which FileFilter you are using (as I told you a few days ago).
 
singh sagar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class TextFilter extends javax.swing.filechooser.FileFilter
{


public boolean accept(File f)
{ return f.isDirectory() || f.getName().endsWith(".txt");}

public String getDescription()
{
return "Accepts txt files only.";
}
}


I am using javax.swing.filechooser.FileFilter.
 
singh sagar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File listFile=new File(FileAddress);

JFileChooser fc = new JFileChooser();
fc.addChoosableFileFilter(new TextFileFilter());
int returnVal = fc.showOpenDialog(this);
this is the code of using the file chooser
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and you have tried it somewhere you can find directories?

Read the API documentation for addChoosableFileFilter, and the links to setFileSelectionMode.

That will explain the problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!