• Post Reply Bookmark Topic Watch Topic
  • New Topic

JFileChooser!  RSS feed

 
Drake Silver
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.
I'm sorry that I'm posting about something that I've found discussed quite a few times already.
ok..so I have a jFileChooser, loadFile.
now, to filter that dialog, this is where things get more confusing.
are there a set of predefined Filters, the All filter won't be any good for me.
I want to make a .txt filter.
I've read that I have to do something like:
loadFile.setFileFilter(new FileFilter());
FileFilter filter = new FileFilter();
filter.addExtension("txt");
filter.setDescription("Text files");
chooser.setFileFilter(filter)
But it won't seem to work.
I've gone to the Java.sun Java tutorial, but her/his method of calling the OpenDilaog is different from mine, and so i'm not sure If I can safely follow his/her footsteps.
I'm going to dig around in my book and on this forum some more.
If no one has responded by then, I'll at least have more answers, and hopefully I'll have already done what needs doing. :roll:
thanks.
 
Jawad Kakar
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where chooser is your JFileChooser

Hope this will help
Jawad
 
Drake Silver
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok...
I've looked and looked.
I found the same thing.
Everything I read pretty much said that you had to have a JFileChooser, and then you would addChoosableFilter(new fileter());
then you describe, set extension, and a few other things I didn't understand.
I tried to do it on my own in 2 diff ways.
everysingle attempt ended in an error.
These are the codes:
Some are commented out, I was testing each one.
JFileChooser loadEmp = new JFileChooser();
//----------------------(1)
/*loadEmp.addChoosableFileFilter(new FileFilter()
{
String description = "Text Files (*.txt)";
String extension = "txt";
public String getDescription()
{
return description;
}
public boolean accept(File f)
{
if(f == null) return false;
if(f.isDirectory()) return true;
return f.getName().toLowerCase().endsWith(extension);
}
});*/
//-----------------------------------(2)
loadEmp.addChoosableFileFilter(new FileFilter());
public boolean accept(File f)
{
if (f.isDirectory())
{
return true;
}
String extension = Utils.getExtension(f);
if (extension != null)
{
if (extension.equals(Utils.txt)
{
return true;
}
else
{
return false;
}
}
return false;
}

//------(3)
/*FileReader in= null;
loadEmp.setFileFilter(new FileFilter());
FileFilter filter = new FileFilter();
filter.addExtension("txt");
filter.setDescription("Text files");
loadEmp.setFileFilter(filter);
if (loadEmp.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
{
File selectedFile = loadEmp.getSelectedFile();
in = new FileReader(selectedFile);
}*/
What makes the first code any different from the 2nd code.
I want to understand what I'm offered, I need to learn this stuff.
[ November 07, 2002: Message edited by: Drake Silver ]
 
Drake Silver
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jawad Kakar:
where chooser is your JFileChooser

Hope this will help
Jawad

Jawad, correct me if i'm wrong, but the way this is written it will only allow me to add one filefilter right?
 
Jawad Kakar
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is right, if you want 2 or more then cut and past the same code with different descripton and extension.
Jawad
 
Drake Silver
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohh....
I see, becuase you have have to add another choosableFileFilter.
ok, that makes perfect sense.
thankyou.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!