Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JFileChooser

 
Miana Husada
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I need help in using JFileChooser.
1.
I want to set the file type so that when the users want to open a file, they can only see <.dat> file.
This is my code:
JFileChooser fc = new JFileChooser();
fc.addChoosableFileFilter(new MyFilter());
fc.setAcceptAllFileFilterUsed(false);
class MyFilter extends javax.swing.filechooser.FileFilter
{
public boolean accept(File file)
{
return file.getAbsolutePath().endsWith (".dat");
}
public String getDescription() {
return "*.dat";
}
}
Problem: file type column has been set in the dialog box, but file chooser can't browse the folder(can't go inside directory and show the desired file).
2.
how to set the save dialog so that user only need to write file name but it is saved by adding the extension also.e.g fileName.dat
Thanks
 
Michael Morris
Ranch Hand
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Miana,
First off this question should have been posted in the swing forum. But I will take a shot at answering your quetsions here.

Problem: file type column has been set in the dialog box, but file chooser can't browse the folder(can't go inside directory and show the desired file).

You need to change your FileFilter.accept() method to accept directories as well like:




2.
how to set the save dialog so that user only need to write file name but it is saved by adding the extension also.e.g fileName.dat

You need to subclass JFileChooser and override the getSelectedFile() and/or getSelectedFiles() methods as appropriate. Something like this:

Here is a sample test class to do this:

Hope this helps,
Michael Morris
 
Miana Husada
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,
thanks for the explanations, it really helps
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic