在 FileChooser 中选择多个文件

发布于 2024-11-26 17:34:44 字数 5345 浏览 1 评论 0原文

如何使用 FileDialog 选择不同目录中的多个文件?

我需要创建一个用户界面,在其中需要添加来自不同目录的大量文件。另外,我需要创建一个复选框来指示选择哪些文件进行进一步操作。

我尝试使用 SWT 来实现这一点,但没有得到预期的结果。事实上,我无法选择每个文件,因为我无法将复选框添加到每个文件。由于我是SWT新手,我很难理解。

我的代码在这里:

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class DirFileSelection {
    Display display = new Display();
    Shell shell = new Shell(display);
    FileDialog fileDialog = new FileDialog(shell, SWT.MULTI);
    DirectoryDialog directoryDialog = new DirectoryDialog(shell);

    String sb = new String("");
    // the label used to display selected dir/file.

    Label label = new Label(shell, SWT.BORDER | SWT.WRAP);

    Button buttonSelectDir;
    Button buttonSelectFile;
    Button validate = new Button(shell, SWT.PUSH);;
    Button reset = new Button(shell, SWT.PUSH);;

    String selectedDir;
    String fileFilterPath = "F:/jdk1.5";

    public DirFileSelection() {

        shell.setSize(1000, 1000);
        label.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        label.setText("Select a dir/file by clicking the buttons below.");

        buttonSelectDir = new Button(shell, SWT.PUSH);
        buttonSelectDir.setText("Select a directory");

        buttonSelectDir.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {

                directoryDialog.setFilterPath(selectedDir);
                directoryDialog.setMessage("Please select a directory and click OK");

                String dir = directoryDialog.open();
                if (dir != null) {

                    // label.setText("Selected dir: " + dir);
                    selectedDir = dir;
                    int decide = 0;
                    System.out.println(sb);
                    String[] str = sb.split("\n");

                    for (int i = 0; i < str.length; i++) {
                        // for (int j = i; j < str.length; j++) {
                        // if(i==j){
                        // continue;
                        // }
                        // else if(str[i].equals(str[j])){
                        // System.out.println("dup");
                        // x=false;
                        // }
                        // }
                        if (dir.equals(str[i])) {
                            System.out.println("dup");
                            decide = 1;
                            break;
                        }
                    }
                    if (decide == 0) {
                        sb = sb.concat(dir + "\n");
                        label.setText(sb);
                    }

                }
                // label.setText(sb.toString());
            }
        });

        buttonSelectFile = new Button(shell, SWT.PUSH);
        buttonSelectFile.setText("Select a file/multiple files");
        buttonSelectFile.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {

                fileDialog.setFilterPath(fileFilterPath);

                fileDialog.setFilterExtensions(new String[] { "*.*" });
                fileDialog.setFilterNames(new String[] { "ALL Files" });

                String firstFile = fileDialog.open();

                if (firstFile != null) {
                    fileFilterPath = fileDialog.getFilterPath();
                    String[] selectedFiles = fileDialog.getFileNames();

                    for (int i = 0; i < selectedFiles.length; i++) {
                        // sb.append(selectedFiles[i] + "\n");
                        sb = sb.concat(selectedFiles[i] + " \n");
                    }

                }
                label.setText(sb.toString());
            }
        });

        validate.setText("Validate");
        validate.addListener(SWT.Selection, new Listener() {

            @Override
            public void handleEvent(Event arg0) {
                // TODO Auto-generated method stub

            }

        });

        reset.setText("Reset");
        reset.addListener(SWT.Selection, new Listener() {

            @Override
            public void handleEvent(Event arg0) {
                fileDialog.setFileName(null);
                System.out.println(fileDialog.getFileName());
                sb = "";
                shell.update();
                label.setText(sb.toString());
            }

        });

        label.setBounds(0, 0, 800, 180);
        buttonSelectDir.setBounds(0, 185, 200, 20);
        buttonSelectFile.setBounds(200, 185, 200, 20);

        validate.setBounds(400, 185, 200, 20);
        reset.setBounds(600, 185, 200, 20);
        shell.pack();
        shell.open();
        // textUser.forceFocus();

        // Set up the event loop.
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                // If no more entries in event queue
                display.sleep();
            }
        }

        display.dispose();
    }

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

How to select multiple files present in different directories using FileDialog?

I need to create a UI where I need to add lots of files from different directories. Also, I need to create a checkbox to indicate which files are selected for further action.

I tried to achieve this using SWT, but I didn't get the desired result. In fact I am not able to select each file since I am not able to add the checkbox to each file. Since I am new to SWT, I have difficulty in understanding.

My code is here:

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class DirFileSelection {
    Display display = new Display();
    Shell shell = new Shell(display);
    FileDialog fileDialog = new FileDialog(shell, SWT.MULTI);
    DirectoryDialog directoryDialog = new DirectoryDialog(shell);

    String sb = new String("");
    // the label used to display selected dir/file.

    Label label = new Label(shell, SWT.BORDER | SWT.WRAP);

    Button buttonSelectDir;
    Button buttonSelectFile;
    Button validate = new Button(shell, SWT.PUSH);;
    Button reset = new Button(shell, SWT.PUSH);;

    String selectedDir;
    String fileFilterPath = "F:/jdk1.5";

    public DirFileSelection() {

        shell.setSize(1000, 1000);
        label.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        label.setText("Select a dir/file by clicking the buttons below.");

        buttonSelectDir = new Button(shell, SWT.PUSH);
        buttonSelectDir.setText("Select a directory");

        buttonSelectDir.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {

                directoryDialog.setFilterPath(selectedDir);
                directoryDialog.setMessage("Please select a directory and click OK");

                String dir = directoryDialog.open();
                if (dir != null) {

                    // label.setText("Selected dir: " + dir);
                    selectedDir = dir;
                    int decide = 0;
                    System.out.println(sb);
                    String[] str = sb.split("\n");

                    for (int i = 0; i < str.length; i++) {
                        // for (int j = i; j < str.length; j++) {
                        // if(i==j){
                        // continue;
                        // }
                        // else if(str[i].equals(str[j])){
                        // System.out.println("dup");
                        // x=false;
                        // }
                        // }
                        if (dir.equals(str[i])) {
                            System.out.println("dup");
                            decide = 1;
                            break;
                        }
                    }
                    if (decide == 0) {
                        sb = sb.concat(dir + "\n");
                        label.setText(sb);
                    }

                }
                // label.setText(sb.toString());
            }
        });

        buttonSelectFile = new Button(shell, SWT.PUSH);
        buttonSelectFile.setText("Select a file/multiple files");
        buttonSelectFile.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {

                fileDialog.setFilterPath(fileFilterPath);

                fileDialog.setFilterExtensions(new String[] { "*.*" });
                fileDialog.setFilterNames(new String[] { "ALL Files" });

                String firstFile = fileDialog.open();

                if (firstFile != null) {
                    fileFilterPath = fileDialog.getFilterPath();
                    String[] selectedFiles = fileDialog.getFileNames();

                    for (int i = 0; i < selectedFiles.length; i++) {
                        // sb.append(selectedFiles[i] + "\n");
                        sb = sb.concat(selectedFiles[i] + " \n");
                    }

                }
                label.setText(sb.toString());
            }
        });

        validate.setText("Validate");
        validate.addListener(SWT.Selection, new Listener() {

            @Override
            public void handleEvent(Event arg0) {
                // TODO Auto-generated method stub

            }

        });

        reset.setText("Reset");
        reset.addListener(SWT.Selection, new Listener() {

            @Override
            public void handleEvent(Event arg0) {
                fileDialog.setFileName(null);
                System.out.println(fileDialog.getFileName());
                sb = "";
                shell.update();
                label.setText(sb.toString());
            }

        });

        label.setBounds(0, 0, 800, 180);
        buttonSelectDir.setBounds(0, 185, 200, 20);
        buttonSelectFile.setBounds(200, 185, 200, 20);

        validate.setBounds(400, 185, 200, 20);
        reset.setBounds(600, 185, 200, 20);
        shell.pack();
        shell.open();
        // textUser.forceFocus();

        // Set up the event loop.
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                // If no more entries in event queue
                display.sleep();
            }
        }

        display.dispose();
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿与i 2024-12-03 17:34:44

我不知道有什么可能的方法可以从不同的文件夹中选择更多文件(不仅在 SWT 中,而且在 Swing 甚至操作系统中)。

您的方法是好的,让用户通过在已选择的文件之后添加新的选定文件(在之前的文件对话框中)来选择他/她想要的任意数量的文件。将其写到 GUI 中文件选择按钮附近。

I'm not aware of any possible way to choose more files from different folders (not just in SWT, but in Swing or even OS's).

Your approach is okey, let user select as many files as he/she wants by adding new selected files after those already selected (in previous file dialogs). Write this down to your GUI near the button for file selection.

梦在深巷 2024-12-03 17:34:44
JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);

这就是您所要做的一切,简单。

JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);

Thats all you have to do, easy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文