Java JFileChooser getSelectedFiles(JFileChooser chooser)

Here you can find the source of getSelectedFiles(JFileChooser chooser)

Description

Workaround for Swing bug: JFileChooser does not support multi-file selection See Sun bug database 4218431.

License

Open Source License

Declaration

public static File[] getSelectedFiles(JFileChooser chooser) 

Method Source Code

//package com.java2s;
/*/* ww  w .j a  v  a 2s  .  c o m*/
 * Copyright (c) 2016 Vivid Solutions.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 *
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */

import java.awt.Container;

import java.io.File;

import javax.swing.JFileChooser;

import javax.swing.JList;

public class Main {
    /**
     * Workaround for Swing bug: JFileChooser does not support multi-file selection
     * See Sun bug database 4218431.
     * http://manning.spindoczine.com/sbe/files/uts2/Chapter14html/Chapter14.htm)
     */
    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;
                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;
    }
}

Related

  1. getOpenFile(String title)
  2. getPropertiesFile(boolean saving, String startName, String extension, String description)
  3. getSaveAsFile(String defaultName, String currentDirectory, String defaultExtension)
  4. getSaveFile(String message, File defaultFileOrDir, String description, final String... extensions)
  5. getSelectedFiles(final JFileChooser chooser)
  6. getSelectedFileWithExtension(JFileChooser c)
  7. getSelectedFileWithExtension(JFileChooser c)
  8. getSystemFile(Component owner, int mode, FileFilter[] filters)
  9. getTextFileChooser()