• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JFileChooser

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Michael,
thanks for the explanations, it really helps
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic