JFileChooser以单击顺序打开多个文件 [英] JFileChooser open multiple files in the order they are clicked

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

问题描述

我有一个应用程序,该应用程序可以接收多个文件并根据其顺序进行某些操作(例如,将它们一个接一个地合并).

I have an application that takes multiple files and applies some operation that depends on their order (e.g. merge them one after another).

用户可以通过Ctrl +单击或Shift +单击以任何顺序选择文件.

The user can select files in any order by Ctrl+click, or by Shift+click.

选择器返回的列表文件与用户单击它们的顺序不同.我希望它们以用户单击它们的顺序返回.

The list files returned by the chooser does not have the same order as the user clicked them. I'd like them to be returned in the same order the user clicked them.

免责声明:我是用户"

Disclaimer: I'm "the user"

我在Windows 7 64位和JDK 7上使用具有Java外观的JFileChooser类.

I'm using the JFileChooser class with Java look and feel on Windows 7 64bits, with JDK 7.

这是一个最小的例子

package choosertest;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class ChooserTest extends JFrame {

    JFileChooser chooser;

    public ChooserTest() {
        chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(true);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        testOpen();
    }

    public static void main(String[] args) {
        new ChooserTest();
    }

    private void testOpen() {
        int choice = chooser.showOpenDialog(this);

        if (choice == JFileChooser.APPROVE_OPTION) {

            File[] inputFiles = chooser.getSelectedFiles();
            for (File f: inputFiles) {
                System.out.println(f.getName());
            }
        }
    }

}

推荐答案

我明白了Ctrl +单击有效,而Shift +单击选择文件向下"有效.

I got to the point that it Ctrl+click works and Shift+click selecting files "downwards" works.

但是,使用Shift +单击选择文件向上"仍然会以错误的顺序添加文件,这可能会造成混淆.

However, using Shift+click to select files "upwards" still adds files in the wrong order, which can be confusing.

此外,该解决方案不会更新文件名"文本字段以反映选择的实际顺序.不过,可能可以通过反射来修复".

Moreover, this solution does not update the "File name" text field to reflect the actual order of the selection. It may be possible to "fix" this using reflection, though.

如果您需要快速解决方案以通过按Ctrl键单击来以正确的顺序选择文件,则可以很好地工作(选择和取消选择).

If you need a quick solution to select files in the correct order by Ctrl-clicking them, this works just fine (both select and deselect).

package choosertest;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class ChooserTest extends JFrame {

    File[] selected;
    JFileChooser chooser;

    public ChooserTest() {
        chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(true);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        testOpen();
    }

    public static void main(String[] args) {
        new ChooserTest();
    }

    private void testOpen() {

        chooser.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals("SelectedFilesChangedProperty")) {
                    if (selected == null) {
                        selected = (File[]) evt.getNewValue();
                    } else {
                        File[] newSelection = (File[]) evt.getNewValue();

                        if (newSelection == null) {
                            selected = null;
                        }
                        // check back and forth to preserve the order of files
                        // as the user added them
                        else {
                            List<File> orderedSel = new LinkedList<>();

                            // add files that are still selected
                            for (File f : selected) {
                                for (File f2 : newSelection) {
                                    if (f.equals(f2)) {
                                        orderedSel.add(f);
                                        break;
                                    }
                                }
                            }

                            Arrays.sort(selected);
                            // add newly selected files
                            for (File f : newSelection) {
                                if (Arrays.binarySearch(selected, f) < 0) {
                                    orderedSel.add(f);
                                }
                            }

                            selected = orderedSel.toArray(
                                    new File[orderedSel.size()]);
                        }
                    }
                    System.out.println(Arrays.toString(selected)); //debug
                }
            }
        });

        // copy previous array of selected files
        File[] prevSelected = null;
        if (selected != null) {
            prevSelected = new File[selected.length];
            System.arraycopy( selected, 0, prevSelected, 0, selected.length );
        }

        int choice = chooser.showOpenDialog(this);

        // if the user did not cancel the selection
        if (choice == JFileChooser.APPROVE_OPTION) {
            System.out.println("FINAL selection: " + Arrays.toString(selected)); //debug
        } else {
            // restore the previous selection
            selected = prevSelected;
            System.out.println("PREVIOUS selection: " + Arrays.toString(selected)); //debug
        }
    }

}

这篇关于JFileChooser以单击顺序打开多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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