Search code examples
javajcomboboxjfilechooserfilefiltercomboboxmodel

How do I set my FileFilter based on a Map in my custom ComboboxModel?


I'll briefly explain what I have going on and then provide some of my code.

I have a small GUI where I select an option on the JComboBox and then click a button that will open a JFileChooser which should filter out the files based on the selection in the JComboBox.

For instance, the user selects Text File from the JComboBox and when the user clicks the button it will open the JFileChooser showing only directories and text files.

In my main class I have this in the constructor:

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

    comboBox.setModel(new FileExtensionModel());
}

Then in that class I have method for the button that opens the 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  
}

And last is my basic extension of the DefaultComboBoxModel:

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

So I'm wondering how I can replace myFileType with the value from the map inside my FileExtensionModel() since I don't have access to it from my inner FileFilter() class.

Any suggestions are welcome, I don't mind moving code around a bit. It would be great if I could handle the majority of it in my FileExtensionModel class.


Solution

  • Can't you create your HashMap outside FileExtensionModel, where it is visible to the FileFilter and pass it to the constructor of `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  
    }
    

    Your FileExtensionModel class:

    public class FileExtensionModel extends DefaultComboBoxModel {
    
        public FileExtensionModel(HashMap select) {
    
            for(String key : select.keySet()) {
               this.addElement(key);
            }
        }
    }
    

    UPDATE

    Or you can create a HashMap of FileFilters, with the extension being the key.

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

    Then create a class for your FileFilter.

    public class MyFileFilter extends FileFiter() {
    
        String extension;
        String description;
    
        public MyFileFilter(String extension, String description) {
            this.extension = extension;
            this.description = description;
       }
    
        ....
    }