java - JFileChooser 中的多种类型选择

标签 java jfilechooser

我的代码:

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 个文件。

enter image description here

最佳答案

在打开对话框之前,您必须启用“多重选择”。

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/

相关文章:

java - 如何将 BlockHound 添​​加到 Spring Boot 应用程序以检测阻塞调用?

java - 如何让进度条 Swing 进度?

Java 小程序的使用

java - jFileChooser 与任务计划程序一起使用时不工作

java - JFileChooser 中的滚动条出错

java - JFileCoser 按选择顺序选择多个文件

java - 在文件选择器中禁用“新建文件夹”按钮无法正常工作

java - 将图像加载到字符串中并在更改图像后保存

java - 树中两个节点之间的最大距离

java - 在同一根上下文下托管静态内容和 JAX-RS 服务