Java 使用 JFileChooser 过滤文件类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19302029/
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-12 15:51:09  来源:igfitidea点击:

Filter file types with JFileChooser

javaswingjfilechooserfilefilter

提问by Brian

I am using JFileChooser to select a file and I am trying to limit the display to show only jpg or jpeg files. I have tried FileFilter and ChoosableFileFilter and it is not limiting the file selection. Here is my code:

我正在使用 JFileChooser 来选择一个文件,我试图将显示限制为仅显示 jpg 或 jpeg 文件。我尝试过 FileFilter 和 ChoosableFileFilter 并且它不限制文件选择。这是我的代码:

JFileChooser chooser = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("JPEG file", new String[] {"jpg", "jpeg"});
chooser.setFileFilter(filter);
chooser.addChoosableFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
  debug.put("You chose to open this file: " + chooser.getSelectedFile().getAbsolutePath());
  File selectedFile = new File(chooser.getSelectedFile().getAbsolutePath());
...

回答by Nihar

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

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

回答by Sanzhar

Try to use fileChooser.setFileFilter(filter)after fileChooser.addChoosableFileFilter(filter), because you need to add your filterto fileChooserand then setting it as default value.

尝试使用fileChooser.setFileFilter(filter)after fileChooser.addChoosableFileFilter(filter),因为您需要添加您的filtertofileChooser然后将其设置为默认值。

Here is the link with good example: http://www.java2s.com/Code/Java/Swing-JFC/CustomizingaJFileChooser.htm

这是带有很好示例的链接:http: //www.java2s.com/Code/Java/Swing-JFC/CustomizingaJFileChooser.htm

回答by makata

Here is a sample code!

这是一个示例代码!

private void btnChangeFileActionPerformed(java.awt.event.ActionEvent evt) {                                              
        final JFileChooser fc = new JFileChooser();
        fc.addChoosableFileFilter(new ArffFilter());
        int returnVal = fc.showOpenDialog(this); 
        ... 
} 

Then

然后

class ArffFilter extends FileFilter {

@Override
public boolean accept(File file) {
    if (file.isDirectory()) {
        return true;
    }
    String fileName = file.getName();
    int i = fileName.lastIndexOf('.');

    if (i > 0 && i < fileName.length() - 1) {
        if (fileName.substring(i + 1).toLowerCase().equals("arff")) {
            return true;
        }
    }

    return false;
}

    @Override
    public String getDescription() {
        return ".arff (Weka format)";
    }
}

回答by Stefan Sprenger

Try this:

尝试这个:

import javax.swing.JFileChooser;


JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileFilter() {

   public String getDescription() {
       return "JPG Images (*.jpg)";
   }

   public boolean accept(File f) {
       if (f.isDirectory()) {
           return true;
       } else {
           String filename = f.getName().toLowerCase();
           return filename.endsWith(".jpg") || filename.endsWith(".jpeg") ;
       }
   }
});

回答by shieldgenerator7

Do you mean "it's not limiting the selection" as in "it's allowing the option for any file type"? If so, then try JFileChooser.setAcceptAllFileFilterUsed(boolean).

您的意思是“它不限制选择”,如“它允许选择任何文件类型”?如果是这样,然后尝试JFileChooser.setAcceptAllFileFilterUsed(boolean)

chooser.setAcceptAllFileFilterUsed(false);

According to the JFileChooser documentation, it should tell it not to add the all-file-types file filter to the file filter list.

根据JFileChooser 文档,它应该告诉它不要将 all-file-types 文件过滤器添加到文件过滤器列表中。