Sam
Sam

Reputation: 1229

Java Swing JFileChooser append files to a JTextArea

I have a browse button which opens a dialog where the user can view directories and files. I am having some trouble appending the files the user selects to a JTextArea. I am trying to do this so the user can select multiple files at a time. The files eventually will be submitted to an Oracle database.

The code I have used for the filechooser is here:

final JFileChooser fc = new JFileChooser();
        JList list = new JList();
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(list)) {
        File file = fc.getSelectedFile();

Can you please show me how to append the files to a JTextArea?

Thanks.

Edit:

I have added the following:

JButton btnBrowse = new JButton("Browse");
        btnBrowse.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                final JFileChooser fc = new JFileChooser();
                fc.setMultiSelectionEnabled(true);
                JList list = new JList();
                fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(list)) {
                     File file = fc.getSelectedFile();
                }   
                for (File file : fc.getSelectedFiles()) {
                       log.append(file.getPath());
                    }
            }
        });

But when selecting browse and choosing multiple files and then selecting open the files are not being displayed within the text area.

Full Code:

package com.example.android.apis.appwidget;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.awt.Color;
import javax.swing.UIManager;

public class VFSTool extends JFrame {

    private JPanel contentPane;
      static private final String newline = "\n";
        JButton openButton, saveButton;
        JTextArea log;
        JFileChooser fc;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    VFSTool frame = new VFSTool();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Tool() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 499, 423);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JLabel lblVfsLoaderTool = new JLabel("Tool");
        lblVfsLoaderTool.setBackground(new Color(255, 255, 153));
        lblVfsLoaderTool.setForeground(UIManager.getColor("Button.darkShadow"));
        lblVfsLoaderTool.setFont(new Font("Copperplate Gothic Light", Font.BOLD, 25));
        lblVfsLoaderTool.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(lblVfsLoaderTool, BorderLayout.NORTH);

        JPanel panel = new JPanel();
        panel.setBackground(UIManager.getColor("Button.darkShadow"));
        contentPane.add(panel, BorderLayout.CENTER);
        panel.setLayout(null);

        JButton btnBrowse = new JButton("Browse");
        btnBrowse.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                final JFileChooser fc = new JFileChooser();
                fc.setMultiSelectionEnabled(true);
                JList list = new JList();
                fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(list)) {
                     File file = fc.getSelectedFile();
                }   
                for (File file : fc.getSelectedFiles()) {
                    log.append(file.getPath() + System.getProperty("line.separator"));
                    }
            }
        });
        btnBrowse.setBounds(107, 185, 97, 25);
        panel.add(btnBrowse);

        JLabel lblCategory = new JLabel("label1");
        lblCategory.setForeground(UIManager.getColor("Button.background"));
        lblCategory.setBounds(12, 13, 82, 25);
        panel.add(lblCategory);

        JComboBox comboBox = new JComboBox();
        comboBox.setForeground(UIManager.getColor("Button.background"));
        comboBox.setBounds(91, 13, 113, 24);
        panel.add(comboBox);

        JLabel lblNewLabel = new JLabel("label2");
        lblNewLabel.setForeground(UIManager.getColor("Button.background"));
        lblNewLabel.setBounds(12, 50, 77, 25);
        panel.add(lblNewLabel);

        JComboBox comboBox_1 = new JComboBox();
        comboBox_1.setBounds(91, 50, 113, 25);
        panel.add(comboBox_1);

        JLabel lblLanguage = new JLabel("label3");
        lblLanguage.setForeground(UIManager.getColor("Button.background"));
        lblLanguage.setBounds(12, 114, 56, 16);
        panel.add(lblLanguage);

        JComboBox comboBox_2 = new JComboBox();
        comboBox_2.setBounds(91, 110, 113, 25);
        panel.add(comboBox_2);

        JCheckBox chckbxIncludeExt = new JCheckBox("include");
        chckbxIncludeExt.setForeground(UIManager.getColor("Button.background"));
        chckbxIncludeExt.setBackground(UIManager.getColor("Button.darkShadow"));
        chckbxIncludeExt.setBounds(12, 219, 113, 25);
        panel.add(chckbxIncludeExt);

        JButton btnSubmit = new JButton("Submit");
        btnSubmit.setBounds(107, 264, 97, 25);
        panel.add(btnSubmit);

        JTextArea textArea = new JTextArea();
        textArea.setBounds(240, 14, 219, 220);
        panel.add(textArea);
    }
}

Upvotes: 0

Views: 2331

Answers (1)

Reimeus
Reimeus

Reputation: 159764

Enable multi-file selection using JFileChooser#setMultiSelectionEnabled, then iterate through all files returned from getSelectedFiles, appending the output of getPath to your JTextArea

if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(list)) {
    for (File file : fc.getSelectedFiles()) {
       myTextArea.append(file.getPath() + System.getProperty("line.separator"));
    }
}

Edit:

You should be getting a stacktrace like this

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at swing9.VFSTool$2.actionPerformed(VFSTool.java:81)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)

This is telling you that something is not initialized, i.e. your JTextArea log

Not only has this not been initialized but hasnt been added to the frame. You probably want the text displayed in textArea instead. This is only available in the scope of the constructor so will need to declared as a class member variable.

private JTextArea textArea;

Upvotes: 3

Related Questions