This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

JFileChooser

 
Greenhorn
Posts: 7
  • 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
 
Ranch Hand
Posts: 3451
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,
thanks for the explanations, it really helps
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic