JFile chooser window?? How do I filter files?

我的梦境 提交于 2019-12-08 09:39:36

问题


In NetBeans, there is an object called a JFileChooser.

I wanted to ask how you can set up a filter in order to just show files that have a .wds extension.

.wds is an extension I use in my program.


回答1:


You have to create a filter class for the *.wds files:

class MyFilter extends javax.swing.filechooser.FileFilter {
    public boolean accept(File file) {
        String filename = file.getName();
        return filename.endsWith(".wds");
    }
    public String getDescription() {
        return "*.wds";
    }
}

then you add the filter to your JFileChooser.

fileChooser.addChoosableFileFilter(new MyFilter());



回答2:


Doesn't anybody believe in reading the API? This is a common requirement and the JDK has a filter class that does this. All you have to do is read the API to find the answer to this question. While you there you can also take a look at the link to the Swing tutorial for other information about file choosers and other Swing components.



来源:https://stackoverflow.com/questions/1876841/jfile-chooser-window-how-do-i-filter-files

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