Java Swing JFileChooser 将文件附加到 JTextArea

标签 java swing

我有一个浏览按钮,可以打开一个对话框,用户可以在其中查看目录和文件。我在将用户选择的文件附加到 JTextArea 时遇到一些问题。我正在尝试这样做,以便用户可以一次选择多个文件。这些文件最终将提交到 Oracle 数据库。

我用于文件选择器的代码在这里:

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();

您能告诉我如何将文件附加到 JTextArea 吗?

谢谢。

编辑:

我添加了以下内容:

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());
                    }
            }
        });

但是,当选择浏览并选择多个文件,然后选择打开时,文件不会显示在文本区域中。

完整代码:

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);
    }
}

最佳答案

使用 JFileChooser#setMultiSelectionEnabled 启用多文件选择,然后迭代从 getSelectedFiles 返回的所有文件,附加 getPath 的输出到您的JTextArea

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

编辑:

你应该得到这样的堆栈跟踪

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)

这告诉您某些东西未初始化,即您的JTextArea log

它不仅没有被初始化,而且还没有被添加到框架中。您可能希望文本显示在 textArea 中。这仅在构造函数的范围内可用,因此需要将其声明为类成员变量。

private JTextArea textArea;

关于Java Swing JFileChooser 将文件附加到 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18801243/

相关文章:

java - 我在哪里可以处理 MVC Java 中的异常?

java - 将匿名内部类转换为 Java 8 lambda

java - 使用代码获取黄色错误/警告

java - 在交付单个 jar 并使用反射时,如何避免在命令行中指定完全限定的类名?

java - 在 JList 上使用 setVisibleRowCount 和 setLayoutOrientation 出现意外结果

java - JTabbedPane+多线程 : Open tab in a new thread

java - Android Studio Tab View 灰色主题颜色

java - 如何编写单元测试框架?

java - 使用新输入更新 JList

Java swing在计算时重新绘制: animating sorting algorithm