Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何基于自定义ComboboxModel中的映射设置文件过滤器?_Java_Jcombobox_Jfilechooser_Filefilter_Comboboxmodel - Fatal编程技术网

Java 如何基于自定义ComboboxModel中的映射设置文件过滤器?

Java 如何基于自定义ComboboxModel中的映射设置文件过滤器?,java,jcombobox,jfilechooser,filefilter,comboboxmodel,Java,Jcombobox,Jfilechooser,Filefilter,Comboboxmodel,我将简要解释我正在进行的工作,然后提供一些代码 我有一个小GUI,在这里我在JComboBox上选择一个选项,然后单击一个按钮,打开一个JFileChooser,它应该根据JComboBox中的选择过滤掉文件 例如,用户从JComboBox中选择文本文件,当用户单击按钮时,它将打开只显示目录和文本文件的JFileChooser 在我的主类中,构造函数中有: public MyApp() { //init components and other logic comboBox.s

我将简要解释我正在进行的工作,然后提供一些代码

我有一个小GUI,在这里我在JComboBox上选择一个选项,然后单击一个按钮,打开一个JFileChooser,它应该根据JComboBox中的选择过滤掉文件

例如,用户从JComboBox中选择
文本文件
,当用户单击按钮时,它将打开只显示目录和文本文件的JFileChooser

在我的主类中,构造函数中有:

public MyApp() {
    //init components and other logic

    comboBox.setModel(new FileExtensionModel());
}
然后在该类中,我有一个打开filechooser的按钮的方法:

private void openFilAction(ActionEvent e) {
    JFileChooser fc = new JFileChooser();

    fc.setFileFilter(new FileFilter() {
        @Override
        public boolean accept(File f) {
            return f.getName().endsWith(myFileType) || f.isDirectory();
        }

        public String getDescription() {
            return "myFileType";
        }
    }

    int choose = fc.showOpenDialog(this);
    //do logic  
}
最后是我对DefaultComboxModel的基本扩展:

public class FileExtensionModel extends DefaultComboBoxModel {
    Map<String, String> selection;

    public FileExtensionModel() {
        selection = new HashMap<String, String>();
        selection.put("Text File", ".txt");
        selection.put("Rar File", ".rar");
        selection.put("Zip File", ".rar");
        selection.put("Tar File", ".tar");
        selection.put("Ini File", ".ini");

        for(String key : select.keySet()) {
           this.addElement(key);
        }
    }
}
公共类FileExtensionModel扩展了DefaultComboxModel{
地图选择;
公共文件扩展模型(){
selection=新HashMap();
选择.put(“文本文件”、“.txt”);
选择.put(“Rar文件”,“.Rar”);
选择.put(“Zip文件”,“.rar”);
选择.put(“Tar文件”,“.Tar”);
选择.put(“Ini文件”,“.Ini”);
对于(字符串键:select.keySet()){
本附录(图例);
}
}
}
因此,我想知道如何用
FileExtensionModel()
中映射的值替换
myFileType
,因为我无法从内部
FileFilter()
类访问它


欢迎提出任何建议,我不介意稍微移动代码。如果我能在FileExtensionModel类中处理大部分,那就太好了。

你不能在
FileExtensionModel
外部创建你的
HashMap
,在那里它对
FileFilter
可见,并将它传递给`FileExtensionModel>的构造函数吗

public class MyClass {

    Map<String, String> selection;
    FileExtensionModel fem;

    MyClass() {
        selection = new HashMap<String, String>();
        selection.put("Text File", ".txt");
        selection.put("Rar File", ".rar");
        selection.put("Zip File", ".rar");
        selection.put("Tar File", ".tar");
        selection.put("Ini File", ".ini");
        fem = new FileExtensionModel(selection);
    }
}

private void openFilAction(ActionEvent e) {
    JFileChooser fc = new JFileChooser();

    fc.setFileFilter(new FileFilter() {
        @Override
        public boolean accept(File f) {
            return f.getName().endsWith(myFileType) || f.isDirectory();
        }

        public String getDescription() {
            for (String key : selection.keySet()) {
               if (selection.get(key).equals(myFileType)) {
                   return key;
               }
            }
            return "";
        }
    }

    int choose = fc.showOpenDialog(this);
    //do logic  
}
更新

或者您可以创建文件过滤器的
HashMap
,扩展名是键

selection = new HashMap<String, String>();
            selection.put("Text File", ".txt");
            selection.put("Rar File", ".rar");
            selection.put("Zip File", ".rar");
            selection.put("Tar File", ".tar");
            selection.put("Ini File", ".ini");
            fem = new FileExtensionModel(selection);

HashMap<String, FileFilter> fileFilters = new HashMap<String, FileFilter>();

for (String key : selection.keySet()) {
    fileFilters.add(new MyFileFilter(key, selection.get(key)));
}

只需将其添加为字段,即可访问
MyApp
类中的
FileExtensionModel
实例。因为它是在构造函数中实例化的,所以这应该不是问题。但是,由于在匿名内部类中使用此字段,因此该字段必须是
final

public class MyApp {
    private final FileExtensionModel fileExtensionModel;
    // ... your other fields

    public MyApp() {
        // ... you other init
        fileExtensionModel = new FileExtensionModel();
        comboBox.setModel(fileExtensionModel);
    }
}
现在,在
openFileAction
方法中,您应该可以访问fileExtensionModel字段,例如提取所选项目(或您需要的任何其他方法)。比如:

myFileType = fileExtensionModel.getSelectedItem();

这是一种可能性,但我仍然无法设置FileFilter,因为
myFileType
是在
FileFilter()
之外声明的。同样,我认为它也不能访问
选择
?我认为
myFileType
是一个字段。然后可以创建另一个HashMap,其中键是扩展名。我将更新答案。感谢更新后的答案,我可以将值从String更改为FileFilter,但这样做是否比像Steiner提到的那样将FileExtensionModel设置为final有好处?每个扩展有一个FileFilter的优点是无需更新其accept和description。然后,如果您愿意,您可以在文件选择器上设置多个文件过滤器。有趣的是,将其设置为最终版本有什么不利之处吗?这似乎比另一个解决方案少了很多工作。在本例中不是这样,不。将其设为最终版本只意味着您无法更改
MyApp
类中的
fileExtensionModel
引用。它需要在实例化
MyApp
类时初始化,以后不能将其设置为其他值。
public class MyApp {
    private final FileExtensionModel fileExtensionModel;
    // ... your other fields

    public MyApp() {
        // ... you other init
        fileExtensionModel = new FileExtensionModel();
        comboBox.setModel(fileExtensionModel);
    }
}
myFileType = fileExtensionModel.getSelectedItem();