Search code examples
javajfilechooserfilefilter

Java JFileChooser filter files for "File.txt"


How can I make a JFileChooser Filter to filter for "File.txt", I can make it filter for just ".txt" but thats not what I want.

I'm going to have a String[] of files to filter for such as

String[] filters = new String[filtersCount];
String[0] = "File.txt";
String[1] = "Text.txt";
String[2] = "uncapText.txt";

I want to be able to browse for only files with those exact names, capitalization does also count so "uncapText.txt" will be accepted but "UncapText.txt" or "unCapText.txt" won't.

This seems like an easy question but I couldn't find any topic on it.


Solution

  • I would suggest writing a filter that checks for...

    return Arrays.asList(filters).contains(file.getName());
    

    In other words, make a List out of your array (to have a nice "contains" method) and then ask that list if the name of the file is contained in it.