开发者

JFile chooser window?? How do I filter files?

开发者 https://www.devze.com 2022-12-14 02:25 出处:网络
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开发者_StackOverflow社区 a .wds extension.

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开发者_StackOverflow社区 a .wds extension.

.wds is an extension I use in my program.


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());


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号