Search code examples
javafor-looparraylistfilechooser

Get all Extensions of the ExtensionFilters of a FileChooser


I want to add an ExtensionFilter (supported Files) in the end with all the extensions of the previous filters to a FileChooser.

This is what I've tried:

FileChooser chooser = new FileChooser();

chooser.getExtensionFilters().addAll(
        new ExtensionFilter("Image Files", "*.png", "*.jpg"),
        new ExtensionFilter("Video Files", "*.mp4")), //And so on...

ArrayList<String> supportedExt = new ArrayList<String>();

for(int x = 0; x < chooser.getExtensionFilters().size(); x++) {

    // And here I get this:
    //The method addAll(Collection<? extends String>) in the type ArrayList<String> is not applicable for the arguments (FileChooser.ExtensionFilter)
    supportedExt.addAll(chooser.getExtensionFilters().get(x)); 
}

chooser.getExtensionFilters().add(new ExtensionFilter("supported Files", supportedExt));

As you can see, I get this error:

The method addAll(Collection) in the type ArrayList is not applicable for the arguments (FileChooser.ExtensionFilter)

Is there another way of doing it?


Solution

  • The compiler is telling you all you need to know. chooser.getExtensionFilters() returns list of FileChooser.ExtensionFilter but your supportedExt is defined to accept String.

    You probably intended:

    supportedExt.addAll( chooser.getExtensionFilters().get(x).getExtensions() );