Java JFileChooser 的文件过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20411919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:45:53  来源:igfitidea点击:

FileFilter for JFileChooser

javaswingjfilechooserfilefilter

提问by Tanay

I want to restrict a JFileChooserto select only mp3 files. But, the following code allows all file types:

我想限制 aJFileChooser只选择 mp3 文件。但是,以下代码允许所有文件类型:

FileFilter filter = new FileNameExtensionFilter("MP3 File","mp3");
fileChooser.addChoosableFileFilter(filter);
fileChooser.showOpenDialog(frame);
File file = fileChooser.getSelectedFile();

采纳答案by Paul Samsotha

Try and use fileChooser.setFileFilter(filter)instead of fileChooser.addChoosableFileFilter(filter);

尝试使用fileChooser.setFileFilter(filter)而不是fileChooser.addChoosableFileFilter(filter);

回答by Amir Afghani

Try:

尝试:

FileFilter filter = new FileNameExtensionFilter("My mp3 description", "mp3");

The first argument is simply a description of the FileNameExtensionFilter- and since the second argument is var args, you can leave it out like you did, effectively meaning there is no filter.

第一个参数只是对FileNameExtensionFilter-的描述,并且由于第二个参数是 var args,您可以像以前一样省略它,实际上意味着没有过滤器。

回答by imanis_tn

This code snippet may help you:

此代码片段可能会帮助您:

JFileChooser jfc=new JFileChooser(System.getProperty("user.dir","."));

FileFilter ff = new FileFilter(){
    public boolean accept(File f){
        if(f.isDirectory()) return true;
        else if(f.getName().endsWith(".mp3")) return true;
            else return false;
    }
    public String getDescription(){
        return "MP3 files";
    }
};

jfc.removeChoosableFileFilter(jfc.getAcceptAllFileFilter());
jfc.setFileFilter(ff);

if(jfc.showDialog(frame,"openG")==JFileChooser.APPROVE_OPTION){
        String fileName = jfc.getSelectedFile().getPath();
}

回答by Samuel

If you want only mp3 files:

如果您只想要 mp3 文件:

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class SalutonFrame {

    public static void main(String[] args) {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setAcceptAllFileFilterUsed(false);
        FileNameExtensionFilter filter = new FileNameExtensionFilter("MPEG3 songs", "mp3");
        fileChooser.addChoosableFileFilter(filter);
        fileChooser.showOpenDialog(null);

    }
}

回答by Sage

fileChooser.addChoosableFileFilter(filter)will add a custom file filter to the list of user-choosable filters. By default, the list of user-choosable filters includes the Accept All filter, which enables the user to see all non-hidden files.

fileChooser.addChoosableFileFilter(filter)将自定义文件过滤器添加到用户可选择的过滤器列表中。默认情况下,用户可选择的过滤器列表包括Accept All 过滤器,它使用户能够查看所有非隐藏文件

You will need to invoke: fileFilter.setAcceptAllFileFilterUsed(false)

您将需要调用: fileFilter.setAcceptAllFileFilterUsed(false)

The setAcceptAllFileFilterUsed(boolean)determines whether the AcceptAll FileFilter is used as an available choice in the choosable filter list. If false, the AcceptAll file filter is removed from the list of available file filters. If true, the AcceptAll file filter will become the the actively used file filter.

setAcceptAllFileFilterUsed(boolean)确定是否将AcceptAll FileFilter用作可选择过滤器列表中一个可用的选择。如果false,则从可用文件过滤器列表中删除 AcceptAll 文件过滤器。如果true,AcceptAll 文件过滤器将成为主动使用的文件过滤器。