JFile chooser window?? How do I filter files?
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.
精彩评论