Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open only .xml file in JFileChooser

I am developing a java application for which I need only .xml files. Now I want to show only .xml files in JFileChooser whenever user wants to save a file or open a existing file.

Is this possible to show only .xml files?

like image 398
Sachin Mhetre Avatar asked Mar 07 '12 05:03

Sachin Mhetre


2 Answers

You can use JFileChooser API to achieve your task.

For Open only .xml file

// create a filechooser;
JFileChooser chooser = new JFileChooser(cwd);
FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter(
     "xml files (*.xml)", "xml");

chooser.setDialogTitle("Open schedule file");
// set selected filter
chooser.setFileFilter(xmlfilter);

Also, go through javax.swing.filechooser.FileNameExtensionFilter.

like image 193
Java Avatar answered Oct 18 '22 21:10

Java


If I remember right, you should use addChoosableFileFilter or setFileFilter method:
http://docs.oracle.com/javase/6/docs/api/javax/swing/JFileChooser.html#addChoosableFileFilter(javax.swing.filechooser.FileFilter)

like image 3
MikhailSP Avatar answered Oct 18 '22 22:10

MikhailSP