FileFilter for JFileChooser

Tanay picture Tanay · Dec 5, 2013 · Viewed 53.7k times · Source

I want to restrict a JFileChooser to select only mp3 files. But, the following code allows all file types:

FileFilter filter = new FileNameExtensionFilter("MP3 File","mp3");
fileChooser.addChoosableFileFilter(filter);
fileChooser.showOpenDialog(frame);
File file = fileChooser.getSelectedFile();

Answer

Paul Samsotha picture Paul Samsotha · Dec 5, 2013

Try and use fileChooser.setFileFilter(filter) instead of fileChooser.addChoosableFileFilter(filter);