Michelle Alexandrova
Michelle Alexandrova

Reputation: 21

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.

Upvotes: 0

Views: 771

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

For one thing, I'd display the multiple file paths in a GUI component better suited to show multiple objects such as a JList. For another, the JFileChooser API will tell you which method returns just a single File and which returns an array of File[]: getSelectedFiles(). Note the s on the end.

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.

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. What you want to do is to call that method within your showTxtFileFrame() method, just as you're currently calling 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());
    }
}

Upvotes: 5

Related Questions