Search code examples
javalinuxjfilechooserrhel

Show only TSTA* files in JFileChooser


So, inverse to the question: Open only .xml file in JFileChooser

I'd like to open a JFileChooser that shows files starting with, for example, "TSTA". I don't care about the rest of the file name.


Solution

  • You can always implement your own FileFilter:

    public class TSTAFileFilter extends FileFilter {
        @Override
        public boolean accept(File f) {
            return f.getName().startsWith("TSTA");
        }
    
        @Override
        public String getDescription() {
            return "Only files starting with TSTA";
        }
    }
    

    And then use it:

    JFileChooser chooser = new JFileChooser(cwd);
    TSTAFileFilter filter = new TSTAFileFilter();
    chooser.setFileFilter(filter);