1

I want to create a new file while file type is choosing by the user on JFileChooser. How this is possible. I use this code for JFileChooser:

JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Save data in your file format");

FileFilter type1 = new ExtensionFilter("Access 2007 Database(*.accdb)", ".accdb");
FileFilter type2 = new ExtensionFilter("Access 2002-2003 Database(*.mdb)", ".mdb");

chooser.addChoosableFileFilter(type1);
chooser.addChoosableFileFilter(type2);

int actionDialog = chooser.showSaveDialog(frame);

Now I an able to get the "File Name" when the user select any file by this code:

 chooser.getSelectedFile().getName();

But I dont know how to get "Files of type" when user select any file. For the convenience I also attach photo of JFileChooser: enter image description here

Vinit ...
  • 1,409
  • 10
  • 37
  • 66

1 Answers1

3

You can get that information using JFileChooser .getFileFilter()

From the Javadocs:

Returns the currently selected file filter

  • I use as mention by you to print on console by using this code: System.out.println(chooser.getFileFilter().toString()); but it print like this: com.xxx.filechooser.ExtensionFilter@1eb904d – Vinit ... May 14 '12 at 07:37
  • @VinitVikash: That's the output of the default `toString()` implenetation, nothing wrong with that. Please read the Javadocs for `FileFilter` and `ExtensionFileFilter` –  May 14 '12 at 07:40