从JFileChooser获取多个文件 [英] Getting multiple files from JFileChooser

查看:63
本文介绍了从JFileChooser获取多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在使用的GUI应用程序中,我需要选择多个文件,但不是直接使用文件选择器直接打开它,我首先需要将所有必需的文件添加到选定"列表中(这样一来,无需再次选择文件并再次从不同目录中,我可以一次全部选择它们,然后打开添加到该列表的所有文件).此外,我还应该能够从该选定文件"列表中显示的文件中删除多个文件.

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.

使用JFileChooser可以吗,还是我需要根据自己的需求设计一个?

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

推荐答案

您要查找的不是标准功能,但是您可以使用JFileChooser.setAccessory(...)作为参数将JComponent作为自定义选择器.因此,您可以创建一个面板,其中包含一个列表,您可以添加和删除所选文件(或要创建的任何其他JComponent)并将其作为附件添加到文件选择器中.

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.

有关更多说明,请参见 FileChooserDemo2

See the FileChooserDemo2 for more explanation on this.

这是一个例子.我刚刚创建了JList,可以通过选择文件将其添加到其中,并通过从列表中选择文件并单击删除"来删除文件.当您单击打开时,可以从DefaultListModel

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

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();
        }
    }
}

发射器

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());
                    }

                }
            }
        });
    }
}

这篇关于从JFileChooser获取多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆