是否可以使用JFileChooser一次选择多个目录

时间:2013-01-24 20:52:27

标签: java jfilechooser directory

正如标题所述,是否可以使用JFileChooser一次选择多个目录(主目录中的所有子目录),这样我就不必为每个目录重新打开文件选择器窗口了? / p>

1 个答案:

答案 0 :(得分:2)

在问完后我又一次解决了自己的问题。

阻止我以前使用它的事情是我使用多选的检查而不是多选的设置,显然是使用了这个错误以及我一直收到错误。无论如何,工作版本如下:

class AddDirectory implements ActionListener {
    public void actionPerformed(ActionEvent ae) {
        File[] theDir = null;
        theDir = selectDir();
        if(theDir != null) {
            for(File z : theDir) {
                String[] curRow = { z.toString(), "Waiting"};
                dlm.addRow(curRow);
            }
        }
        return;
    }   
    private File[] selectDir() {
        JFileChooser fileChooser = new JFileChooser(lastDir);
        fileChooser.setMultiSelectionEnabled(true);
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int showOpenDialog = fileChooser.showOpenDialog(null);
        if (showOpenDialog != JFileChooser.APPROVE_OPTION) {
            return null;
        }
        File[] uploadDir = fileChooser.getSelectedFiles();
        lastDir = new File(uploadDir[uploadDir.length-1].getParent());
        return uploadDir;
    }
}

一旦我获得目录,就会将它们加载到JTable中进行修改,然后再运行其余的代码。

相关问题