views:

73

answers:

2

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.

+2  A: 

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());
dusan
Sorry about the down vote, I probably shouldn't have done it since the answer is technically correct. I was going to downvote for spoon feeding an answer rather than referring to the API where the answer is clearly given (see my comment below). It is now too late to remove the down vote. Posters need to learn to develop problem solving skills, and the first of those should be to read the API or tutorial.
camickr
+1  A: 

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.

camickr