[ Back | Previous | Next ]

How to select multiple files with JFileChooser?

Package:
javax.swing.*
Product:
Swing
Release:
1.2
Related Links:
ComboBox
JDialog
JFileChooser
JFrame
JOptionPane
JProgressBar
JScrollPane
JTable
General
JTree
JWindow
KeyStroke
LayeredPane
UIDefaults
Comment:
public class MultiFileChooser extends JFileChooser {     
  public MultiFileChooser() {
    }     
// Workaround for @^%#&$* missing functionality in Swing's JFileChooser
    // (from http://manning.spindoczine.com/sbe/files/uts2/Chapter14html/Chapter14.htm)
    public File[] getSelectedFiles() {
        Container c1 = (Container)getComponent(3);
        JList list = null;
        while (c1 != null) {
            Container c = (Container)c1.getComponent(0);
            if (c instanceof JList) {
                list = (JList)c;
                break;
            }
            c1 = c;
        }
        Object[] entries = list.getSelectedValues();
        File[] files = new File[entries.length];
        for (int k=0; k < entries.length; k++) {
            if (entries[k] instanceof File)
                files[k] = (File)entries[k];
        }
        return files;
    }
}
1