FileFilter for JFileChooser
Asked Answered
C

5

21

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();
Conventual answered 5/12, 2013 at 22:14 Comment(2)
Did not try, but according to the Javadoc for the constructor, the first parameter is just the name of the filter; accepted extensions start with parameter no. two.Reiterate
I tried that too, but it do not works!Conventual
O
31

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

Older answered 5/12, 2013 at 22:40 Comment(0)
S
20

If you want only mp3 files:

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class SalutonFrame {

    public static void main(String[] args) {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setAcceptAllFileFilterUsed(false);
        FileNameExtensionFilter filter = new FileNameExtensionFilter("MPEG3 songs", "mp3");
        fileChooser.addChoosableFileFilter(filter);
        fileChooser.showOpenDialog(null);

    }
}
Sauers answered 5/12, 2013 at 22:34 Comment(1)
finally some solution that actually works for me - upvoted, thank you!Sheathe
O
9

Try:

FileFilter filter = new FileNameExtensionFilter("My mp3 description", "mp3");

The first argument is simply a description of the FileNameExtensionFilter - and since the second argument is var args, you can leave it out like you did, effectively meaning there is no filter.

Oversleep answered 5/12, 2013 at 22:29 Comment(0)
M
4

fileChooser.addChoosableFileFilter(filter) will add a custom file filter to the list of user-choosable filters. By default, the list of user-choosable filters includes the Accept All filter, which enables the user to see all non-hidden files.

You will need to invoke: fileFilter.setAcceptAllFileFilterUsed(false)

The setAcceptAllFileFilterUsed(boolean) determines whether the AcceptAll FileFilter is used as an available choice in the choosable filter list. If false, the AcceptAll file filter is removed from the list of available file filters. If true, the AcceptAll file filter will become the the actively used file filter.

Merna answered 5/12, 2013 at 23:4 Comment(1)
Thank you ! Just a remark: setAcceptAllFileFilterUsed() belong to the JFileChooser class ;)Brimmer
V
3

This code snippet may help you:

JFileChooser jfc=new JFileChooser(System.getProperty("user.dir","."));

FileFilter ff = new FileFilter(){
    public boolean accept(File f){
        if(f.isDirectory()) return true;
        else if(f.getName().endsWith(".mp3")) return true;
            else return false;
    }
    public String getDescription(){
        return "MP3 files";
    }
};

jfc.removeChoosableFileFilter(jfc.getAcceptAllFileFilter());
jfc.setFileFilter(ff);

if(jfc.showDialog(frame,"openG")==JFileChooser.APPROVE_OPTION){
        String fileName = jfc.getSelectedFile().getPath();
}
Vshaped answered 5/12, 2013 at 22:30 Comment(1)
This functionality is already build in with the 'FileNameExtensonFilter' class...Moncada

© 2022 - 2024 — McMap. All rights reserved.