Open only .xml file in JFileChooser

孤者浪人 提交于 2019-12-05 00:35:50

问题


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?


回答1:


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.




回答2:


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)



来源:https://stackoverflow.com/questions/9596468/open-only-xml-file-in-jfilechooser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!