9

I have a question about the JFileChooser in Swing. I'm trying to get multiple file extensions in the drop-down box, but have no idea how to do it.

There is the method

extFilter = FileNameExtensionFilter(description, extensions);

that I can then use by writing

fileChooser.setFileFilter(extFilter);

however, as you can see, this only supports one option in the drop-down list. How do I add more?

Peter Lang
  • 54,264
  • 27
  • 148
  • 161
bcoughlan
  • 25,987
  • 18
  • 90
  • 141

4 Answers4

18

I think you want the addChoosableFileFilter method. Read the tutorial.

You can simply create a filter that subclasses FileFilter and call the method I outlined above with that filter as an argument.

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
4

I'm sure you have already solved this to your satisfaction, but I came across this link at the same time I came across your question. In particular, look at section 3 for a way to generate multiple filters easily.

clartaq
  • 5,320
  • 3
  • 39
  • 49
  • 1
    even tough this answer has more than 3 years was surprisingly useful, having a class extending the filechooser class to add your own behavior it's way useful! – Abstract Aug 23 '13 at 22:20
1

use

filter.addChoosableFileFilter(new FileNameExtensionFilter(description, extensions));

as many as the number of your extensions.

1

I know this question was asked long back, but one can take the following steps to define multiple extensions:

JFileChooser jfc = new JFileChooser(); jfc.setFileFilter(new FileNameExtensionFilter("Select XSL Files","xsl","xslt"));

Geebee
  • 113
  • 3
  • 9