java 无法让 FileNameExtensionFilter 工作

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

Can not get FileNameExtensionFilter to work

javajfilechooser

提问by Aqeel Hussain

I can not get my JFileChooser to show only the extensions I want (text files).

我无法让我的 JFileChooser 只显示我想要的扩展名(文本文件)。

JFileChooser fc = new JFileChooser();
            FileNameExtensionFilter textFilter = new FileNameExtensionFilter("Text Files","txt");
            fc.addChoosableFileFilter(textFilter);

What am I doing wrong? if I remove the filter the text files show up. Question might seem silly, but I have tried to find a solution and my code looks the same as other examples. Maybe I am just brain-locked.

我究竟做错了什么?如果我删除过滤器,则会显示文本文件。问题可能看起来很愚蠢,但我试图找到解决方案,我的代码看起来与其他示例相同。也许我只是脑子被锁住了。

Thank you for your time

感谢您的时间

I tried the suggested approach and I still have the same problem. So there must be something wrong elsewhere. I will load the class code - maybe someone can spot what I am doing wrong:

我尝试了建议的方法,但仍然遇到同样的问题。所以一定是其他地方出了问题。我将加载课程代码 - 也许有人可以发现我做错了什么:

import java.awt.event.ActionEvent;

 import java.awt.event.ActionListener;
 import javax.swing.JButton;
 import javax.swing.JFileChooser;
 import javax.swing.JFrame;
 import javax.swing.filechooser.FileNameExtensionFilter;


public class ButtonListener implements ActionListener {

private JFrame fr;

public ButtonListener (JFrame frame){
    fr = frame;
}

public void actionPerformed(ActionEvent event) {
    if(event.getSource() instanceof JButton) {
        String action = event.getActionCommand();

        if (action.equals("First text")){

            JFileChooser fc = new JFileChooser();
            FileNameExtensionFilter textFilter = new FileNameExtensionFilter("Text Files","txt");
            fc.addChoosableFileFilter(textFilter);
            int returnVal = fc.showOpenDialog(fr);
            }
        else if (action.equals("Second text")){

            }
        else {
            System.out.println("Error in ButtonListener");
        }
    }

}

}

}

It is not finished, but it should still be able to handle then extension issues that I am having.

它还没有完成,但它应该仍然能够处理我遇到的扩展问题。

回答by Thorn

Try using setFileFilterinstead.

尝试使用setFileFilter

I never used FileNameExtensionFilter, but it's nice that they added this convenience class to Java 1.6

我从未使用过 FileNameExtensionFilter,但很高兴他们将这个便利类添加到 Java 1.6

I have always just extended FileFilter and then override accept. Using my class defined below, you could then write

我一直只是扩展 FileFilter 然后覆盖接受。使用我在下面定义的类,然后您可以编写

chooser.setFileFilter(new OpenFileFilter("txt"));


/**
 * This class defines which file types are opened (by default) by the program.
 * This file filter is used to associate a single file type (extension) with the program.
 * You could add more than one file type to the open file dialog using this class by repeatedly
 *    calling addFileFilter.
 */
import java.io.File;
import javax.swing.filechooser.*;

public class OpenFileFilter extends FileFilter {
    public String fileExt = "";
    String txtExt = ".txt";

    public OpenFileFilter() {
        this(".pxml");  //default file type extension.
    }

    public OpenFileFilter(String extension) {
        fileExt = extension;
    }

     @Override public boolean accept(File f) {
        if (f.isDirectory())
            return true;
        return  (f.getName().toLowerCase().endsWith(fileExt)); 
    }

    public String getDescription() {
        if(fileExt.equals(txtExt ))
            return  "Text Files (*" + fileExt + ")";
        else
            return ("Other File");
    }
}

回答by Aqeel Hussain

I know your question was already answered, but just in case you didn't know... You could have also use you FileNameExtensionFilter and just do:

我知道您的问题已经得到解答,但以防万一您不知道...您也可以使用 FileNameExtensionFilter 并执行以下操作:

fc.setFileFilter(textFilter);

I was stuck on the same prob. as you for a while. ;)

我被困在同一个问题上。像你一会儿。;)