How can I open multiple files at a time using FileChooser or FileDialogBox in Swing?

Sandip Chitale

First of all FileChooser does not open files. It simply returns the files selected by the user.

The problem is the -

public File[] getSelectedFiles();

of JFileChooser does not work.

You can subclass JFileChooser and override the method getSelectedFiles() method. Here is the hacked method -

public static File[] getSelectedFiles(JFileChooser chooser) {
	// Although JFileChooser won't give us this information,
	// we need it...
	Container c1 = (Container)chooser.getComponent(3);
	JList list = null;
	while (c1 != null) {
		Container c = (Container)c1.getComponent(0);
		if (c instanceof JList) {
			list = (JList)c;
		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;

as given in http://manning.spindoczine.com/sbe/files/uts2/Chapter14html/Chapter14.htm