-1

So for a project I'm making I need to import pictures. But the only problem is that my filter, filters everything and I never recieve A file back

public class FileChooser extends JPanel{

    public FileChooser(){

        JFileChooser fileopen=new JFileChooser();
        FileFilter filter=new FileNameExtensionFilter("jpg files","jpg");
        fileopen.addChoosableFileFilter(filter);
        int ret =fileopen.showDialog(null,"Open file");

        if (ret==JFileChooser.APPROVE_OPTION){
            File[] picture=fileopen.getSelectedFiles();
            System.out.println(picture[0].getName());
        }
    }
}
4

1 回答 1

2

查看javadocJFileChooser#getSelectedFiles

如果文件选择器设置为允许多选,则返回选定文件的列表

因此,启用多选选项JFileChooser fileopen以便对话框可以返回选定的File数组列表....否则结果数组将始终为空,并且ArrayIndexOutOfBoundsException在尝试访问第一个元素时将导致

fileopen.setMultiSelectionEnabled(true); 
于 2013-08-12T13:06:38.470 回答