如何将 CSV 文件读入文本窗格 Java
How to Read a CSV file onto a Text Pane Java
好的,所以我正在为 class 做一个项目,我已经得到了我需要工作的东西(现在。)我现在想做的是当我点击 gui 上的按钮时它在文本窗格中显示所有数据。到目前为止,它所做的只是打印出成绩(我明白为什么)但我希望它打印出 CSV 文件中正在解析的内容。我并没有要求任何人神奇地解决我的整个代码我已经记下了大部分代码,我只是不知道如何让按钮实际显示所需的结果这让我很生气因为我终于得到了代码来做什么我想要,但我看不到结果。 (它在控制台中有效,但是当 .jar 是 运行 时,什么都不显示。)
问题已得到解答阅读下面的评论。代码已被删除。感谢您的宝贵时间!
您不应该在每次单击 JButton
时都创建一个新的 JTextPane
。将窗格一次添加到 JFrame
,然后在 ActionListener
中通过 setText:
设置值
您从未通过 setText:
将 csv 文件的内容设置到您的 JTextPane
,但您仅通过 System.out.println("Student - " + grades[0] + " "+ grades[1] + " Grade - " + grades[2]);
这里给大家举个例子。该示例并没有真正基于您发布的代码,因为查看您的整个代码并更正它会花费很多精力,但它显示了您需要做的所有事情才能使您的代码正常工作。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Instructor {
public static void main(String[] args) {
JFrame frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setLayout(new BorderLayout());
final JTextPane textPane;
textPane = new JTextPane();
frame.add(textPane,BorderLayout.CENTER);
JButton button = new JButton("Read CSV");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Reset textpanes content so on every button click the new content of the read file will be displayed
textPane.setText("");
String fileResult = "";
try {
BufferedReader csvReader = new BufferedReader(new FileReader("myCSV.csv"));
String line = null;
while ((line = csvReader.readLine()) != null) {
//Do your logic here which information you want to parse from the csv file and which information you want to display in your textpane
fileResult = fileResult + "\n" +line;
}
}
catch(FileNotFoundException ex) {
System.err.println("File was not found");
}
catch(IOException ioe) {
System.err.println("There was an error while reading the file");
}
textPane.setText(fileResult);
}
});
frame.add(button,BorderLayout.SOUTH);
frame.setVisible(true);
}
}
那我做了什么:
- 创建
JTextpane
不是在 ActionListener
而是一次
- 读取
ActionListener
中的文件并提供try
和catch
以确保在找不到文件或类似这样的情况下有一些错误处理.
- 不要通过
System.out.println();
打印 csv 的结果,而是通过调用 setText:
方法将结果设置到 JTextPane
。
我还添加了一个 LayoutManger
(BorderLayout
),而不是将 JTextPane
和按钮添加到带有 NullLayout
的 JFrame
。这不是问题的一部分,但如果你有时间,你应该改变它,因为 NullLayout
根本不推荐(setBounds
在你的代码中)。
好的,所以我正在为 class 做一个项目,我已经得到了我需要工作的东西(现在。)我现在想做的是当我点击 gui 上的按钮时它在文本窗格中显示所有数据。到目前为止,它所做的只是打印出成绩(我明白为什么)但我希望它打印出 CSV 文件中正在解析的内容。我并没有要求任何人神奇地解决我的整个代码我已经记下了大部分代码,我只是不知道如何让按钮实际显示所需的结果这让我很生气因为我终于得到了代码来做什么我想要,但我看不到结果。 (它在控制台中有效,但是当 .jar 是 运行 时,什么都不显示。)
问题已得到解答阅读下面的评论。代码已被删除。感谢您的宝贵时间!
您不应该在每次单击
JButton
时都创建一个新的JTextPane
。将窗格一次添加到JFrame
,然后在ActionListener
中通过setText:
设置值
您从未通过
setText:
将 csv 文件的内容设置到您的JTextPane
,但您仅通过System.out.println("Student - " + grades[0] + " "+ grades[1] + " Grade - " + grades[2]);
这里给大家举个例子。该示例并没有真正基于您发布的代码,因为查看您的整个代码并更正它会花费很多精力,但它显示了您需要做的所有事情才能使您的代码正常工作。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Instructor {
public static void main(String[] args) {
JFrame frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setLayout(new BorderLayout());
final JTextPane textPane;
textPane = new JTextPane();
frame.add(textPane,BorderLayout.CENTER);
JButton button = new JButton("Read CSV");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Reset textpanes content so on every button click the new content of the read file will be displayed
textPane.setText("");
String fileResult = "";
try {
BufferedReader csvReader = new BufferedReader(new FileReader("myCSV.csv"));
String line = null;
while ((line = csvReader.readLine()) != null) {
//Do your logic here which information you want to parse from the csv file and which information you want to display in your textpane
fileResult = fileResult + "\n" +line;
}
}
catch(FileNotFoundException ex) {
System.err.println("File was not found");
}
catch(IOException ioe) {
System.err.println("There was an error while reading the file");
}
textPane.setText(fileResult);
}
});
frame.add(button,BorderLayout.SOUTH);
frame.setVisible(true);
}
}
那我做了什么:
- 创建
JTextpane
不是在ActionListener
而是一次 - 读取
ActionListener
中的文件并提供try
和catch
以确保在找不到文件或类似这样的情况下有一些错误处理. - 不要通过
System.out.println();
打印 csv 的结果,而是通过调用setText:
方法将结果设置到JTextPane
。
我还添加了一个 LayoutManger
(BorderLayout
),而不是将 JTextPane
和按钮添加到带有 NullLayout
的 JFrame
。这不是问题的一部分,但如果你有时间,你应该改变它,因为 NullLayout
根本不推荐(setBounds
在你的代码中)。