Tommus
Tommus

Reputation: 83

Is there a workaround for: FileFilter doesn't work in Windows?

The following JFileChooser code works fine, except that the FileFilter doesn't filter. It doesn't do anything. From another stackoverflow answer: "Filename filters do not function in Sun's reference implementation for Microsoft Windows." Comment from Nov 21st, 2016

Is there a FileFilter workaround for Windows?

public String getPathFileName(String startingDir) {
   String returnSelectedFile = "";
   JFileChooser fileChooser = new JFileChooser(startingDir);
   FileFilter filter = new FileNameExtensionFilter("Excel file", "xls", "xlsx");
   fileChooser.addChoosableFileFilter(filter);
   int returnValue = fileChooser.showOpenDialog(null);
   if (returnValue == JFileChooser.APPROVE_OPTION) {
       File selectedFile = fileChooser.getSelectedFile();
       returnSelectedFile = selectedFile.getPath();
   }
   return returnSelectedFile;
}

Upvotes: 0

Views: 265

Answers (1)

Nick Clark
Nick Clark

Reputation: 1446

I have found this to work:

final JFileChooser chooser = new JFileChooser(); chooser.setFileFilter(new FileNameExtensionFilter("CSV FILES", "csv"));

I have found that this works for one file filter, but I cannot confirm for multiple file filters. Hope this helps.

Upvotes: 3

Related Questions