简体   繁体   English

JFileChooser在JTextField中显示多个选定的文件

[英]JFileChooser show multiple selected files in JTextField

final JFileChooser fc = new JFileChooser();
   File[] files = fc.getSelectedFiles();

   private void showTxtFileFrame() {
       fc.setMultiSelectionEnabled(true);
       fc.setCurrentDirectory(new File(System.getProperty("user.home")));
       int result = fc.showOpenDialog(this);   
       if(result == JFileChooser.APPROVE_OPTION) {
            textfield1.setText(fc.getSelectedFile().getAbsolutePath());
    }

I want to select multiple files and have them listed on my text field. 我想选择多个文件,并在我的文本字段中列出它们。 I am able to selected multiple files but it only shows the absolute path of a single file. 我可以选择多个文件,但它只显示单个文件的绝对路径。

For one thing, I'd display the multiple file paths in a GUI component better suited to show multiple objects such as a JList. 一方面,我将在GUI组件中显示多个文件路径,该路径更适合于显示JList等多个对象。 For another, the JFileChooser API will tell you which method returns just a single File and which returns an array of File[] : getSelectedFiles() . 另外, JFileChooser API会告诉您哪个方法仅返回单个File,哪个方法返回File[]的数组: getSelectedFiles() Note the s on the end. 注意s就结束。

But of course, you can't put an array into a JTextField, but I'm guessing that you know what to do with the data once you get it. 但是,当然,您不能将数组放入JTextField中,但是我猜测一旦获得数据,您就知道如何处理数据。

Also, this makes no sense: 另外,这没有任何意义:

final JFileChooser fc = new JFileChooser();
File[] files = fc.getSelectedFiles();

Since you're actually calling getSelectedFiles() before displaying the file chooser dialog. 由于实际上是在显示文件选择器对话框之前调用getSelectedFiles() What you want to do is to call that method within your showTxtFileFrame() method, just as you're currently calling getSelectedFile() . 您想要做的就是 showTxtFileFrame()方法中调用该方法,就像您当前正在调用getSelectedFile()


For example: 例如:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.*;

@SuppressWarnings("serial")
public class JFileChooserExample extends JPanel {
    // use a list model and JList that works *directly* with Files
    private DefaultListModel<File> fileListModel = new DefaultListModel<>();
    private JList<File> fileJList = new JList<>(fileListModel);

    public JFileChooserExample() {
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new JButton(new SelectFilesAction("Select Files", KeyEvent.VK_S)));

        // help set the width and height of the JList
        fileJList.setVisibleRowCount(10);
        fileJList.setPrototypeCellValue(new File("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
        JScrollPane scrollPane = new JScrollPane(fileJList);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
        setLayout(new BorderLayout(3, 3));
        add(buttonPanel, BorderLayout.PAGE_START);
        add(scrollPane, BorderLayout.CENTER);
    }

    private class SelectFilesAction extends AbstractAction {
        public SelectFilesAction(String name, int mnemonic) {
            super(name);
            putValue(MNEMONIC_KEY, mnemonic);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser fc = new JFileChooser();
            fc.setMultiSelectionEnabled(true);
            fc.setCurrentDirectory(new File(System.getProperty("user.home")));
            int result = fc.showOpenDialog(JFileChooserExample.this);   
            if(result == JFileChooser.APPROVE_OPTION) {
                fileListModel.clear();  // clear the model of prior files
                 File[] files = fc.getSelectedFiles();
                 for (File file : files) {
                     // add all files to the model
                    fileListModel.addElement(file);
                }
            }
        }
    }

    private static void createAndShowGui() {
        JFileChooserExample mainPanel = new JFileChooserExample();

        JFrame frame = new JFrame("JFileChooser Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM