我的代码:
JFileChooser dialog = new JFileChooser();
dialog.showOpenDialog(rootPane);
dialog.setMultiSelectionEnabled(true);
dialog.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean accept(File arg0) {
return arg0.getAbsolutePath().endsWith("png")||
arg0.getAbsolutePath().endsWith("jpg");
}
});
File[] file = dialog.getSelectedFiles();
程序运行时,我无法选择超过 1 个文件。
最佳答案
在打开对话框之前,您必须启用“多重选择”。
dialog.setMultiSelectionEnabled(true);
dialog.showOpenDialog(rootPane);
与设置文件过滤器相同。
JFileChooser dialog = new JFileChooser();
dialog.setMultiSelectionEnabled(true);
dialog.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean accept(File arg0) {
return arg0.getAbsolutePath().endsWith("png")
|| arg0.getAbsolutePath().endsWith("jpg");
}
});
dialog.showOpenDialog(rootPane);//open it last
File[] file = dialog.getSelectedFiles();
关于java - JFileChooser 中的多种类型选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36963387/