Open only .xml file in JFileChooser

Sachin Mhetre picture Sachin Mhetre · Mar 7, 2012 · Viewed 13.9k times · Source

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?

Answer

Java picture Java · Mar 7, 2012

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.