JFile选择器窗口?如何过滤文件? [英] JFile chooser window?? How do I filter files?

查看:103
本文介绍了JFile选择器窗口?如何过滤文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在NetBeans中,有一个名为JFileChooser的对象.

In NetBeans, there is an object called a JFileChooser.

我想问一下如何设置过滤器以仅显示扩展名为.wds的文件.

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

.wds是我在程序中使用的扩展名.

.wds is an extension I use in my program.

推荐答案

您必须为* .wds文件创建一个过滤器类:

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";
    }
}

然后将过滤器添加到JFileChooser.

then you add the filter to your JFileChooser.

fileChooser.addChoosableFileFilter(new MyFilter());

这篇关于JFile选择器窗口?如何过滤文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆