Getting multiple files from JFileChooser

孤街浪徒 提交于 2019-12-17 20:26:24

问题


In a GUI app that I am working on, I require to select multiple files but instead of directly opening it with the File chooser I first need to add all required files in a Selected list (so that instead of selecting files again and again from different directories I can select them all at a time and then open all the files added to that list). Moreover I should also be able to remove multiple files from those present in that Selected File list too.

Is that possible with JFileChooser or do I need to design one as per my requirements?


回答1:


What you are looking for is not a standard feature, but you can customize the chooser, using JFileChooser.setAccessory(...) which takes as a argument a JComponent. So you can create a panel to with a list that you can add and remove selected files (or any other JComponent you want to create) and add it as an accessory to the file chooser.

See the FileChooserDemo2 for more explanation on this.

Here's an example. I just created JList that you can add to by selecting files, and remove files by selecting the file from the list and clicking remove. When you click open, all the files can be obtained from the DefaultListModel

FileListAccessory class that extends JComponent

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

public class FileListAccessory extends JComponent implements PropertyChangeListener {

    private File file = null;
    private DefaultListModel model;
    private JList list;
    private JButton removeItem;

    public FileListAccessory(JFileChooser chooser) {
        chooser.addPropertyChangeListener(this);

        model = new DefaultListModel();
        list = new JList(model);
        JScrollPane pane = new JScrollPane(list);
        pane.setPreferredSize(new Dimension(200, 250));

        removeItem = createRemoveItemButton();

        setBorder(new EmptyBorder(10, 10, 10, 10));
        setLayout(new BorderLayout());
        add(pane);
        add(removeItem, BorderLayout.SOUTH);

    }

    public DefaultListModel getModel() {
        return model;
    }

    private void addFileToList() {
        model.addElement(file);
    }

    private void removeFileFromList() {
        if (list.getSelectedIndex() != -1) {
             model.remove(list.getSelectedIndex());
        }
    }

    private JButton createRemoveItemButton() {
        JButton button = new JButton("Remove");
        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                removeFileFromList();
            }
        });
        return button;
    }

    @Override
    public void propertyChange(PropertyChangeEvent e) {
        boolean update = false;
        String prop = e.getPropertyName();

        //If the directory changed, don't do anything
        if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
            file = null;
            update = true;
            //If a file became selected, find out which one.
        } else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
            file = (File) e.getNewValue();
            update = true;
        }

        if (update && file != null) {
            addFileToList();
        }
    }
}

Launcher

import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;

public class JavaApplication4 {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFileChooser fc = new JFileChooser();
                FileListAccessory accessory = new FileListAccessory(fc);
                fc.setAccessory(accessory);

                int open = fc.showOpenDialog(fc);
                if (open == JFileChooser.APPROVE_OPTION) {
                    DefaultListModel model = accessory.getModel();
                    for (int i = 0; i < model.getSize(); i++) {
                        System.out.println(((File)model.getElementAt(i)).getName());
                    }

                }
            }
        });
    }
}



回答2:


but instead of directly opening it with the File chooser

A file chooser doesn't open a file, it just selects a file.

I first need to add all required files in a Selected list

I would add the selected files to a JList.

Moreover I should also be able to remove multiple files from those present in that Selected File list too.

You can dynamically add/remove files from a JList. Read the section from the Swing tutorial on How to Use Lists for more information and working examples.




回答3:


You will need:

chooser.setMultiSelectionEnabled(true);
File selected[] = chooser.getSelectedFiles();


来源:https://stackoverflow.com/questions/24290210/getting-multiple-files-from-jfilechooser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!