java – 从CSV文件读取数据并将其显示在JTable中
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 从CSV文件读取数据并将其显示在JTable中,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4399字,纯文字阅读大概需要7分钟。
内容图文
![java – 从CSV文件读取数据并将其显示在JTable中](/upload/InfoBanner/zyjiaocheng/816/d564587183f345c79d1bafccce8b6340.jpg)
我试图从CSV文件中读取数据并将其显示在JTable上,但我有一些问题.我是菜鸟,请耐心等待.我查看并合并了几个来源的示例代码,但无济于事.该表显示但是它是空白的.我知道我正在阅读数据,因为我可以打印它.我怀疑我的ModelTable设置有问题.任何帮助将不胜感激.
package t1data;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.*;
import javax.swing.border.EmptyBorder;
import java.io.*;
import javax.swing.table.*;
public class T1Data extends JPanel implements ActionListener {
public T1Data() {
super(new BorderLayout(3,3));
JTable Table = new JTable(new MyModel());
Table.setPreferredScrollableViewportSize(new Dimension(700, 70));
Table.setFillsViewportHeight(true);
JPanel ButtonOpen = new JPanel( new FlowLayout(FlowLayout.CENTER) );
JButton OpenFile = new JButton("Open");
JButton SaveFile = new JButton("Save");
ButtonOpen.add(OpenFile);
add(ButtonOpen, BorderLayout.SOUTH);
OpenFile.addActionListener(this);
SaveFile.addActionListener(this);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(Table);
//Add the scroll pane to this panel.
add(scrollPane, BorderLayout.CENTER);
// add a nice border
setBorder(new EmptyBorder(5,5,5,5));
}
public void actionPerformed (ActionEvent Event) {
JFileChooser fc = new JFileChooser();
boolean pressed = true;
// if (Event.getSource() == OpenFile) {
if (pressed) {
int ReturnVal = fc.showOpenDialog(null);
// if (ReturnVal == JFileChooser.APPROVE_OPTION) {
CSVFile Rd = new CSVFile();
//ArrayList<String[]> Rs2 = new ArrayList<>();
MyModel NewModel = new MyModel();
File DataFile = fc.getSelectedFile();
ArrayList<String[]> Rs2 = Rd.ReadCSVfile(DataFile);
NewModel.AddCSVData(Rs2);
System.out.println ("Rows: " +NewModel.getRowCount());
System.out.println ("Cols: " +NewModel.getColumnCount());
}
}
// Method for reading CSV file
public class CSVFile {
private ArrayList<String[]> Rs = new ArrayList<>();
private String[] OneRow;
public ArrayList<String[]> ReadCSVfile (File DataFile) {
try {
BufferedReader brd = new BufferedReader (new FileReader(DataFile));
while (brd.readLine() != null) {
String st = brd.readLine();
OneRow = st.split(",|\\s|;");
Rs.add(OneRow);
System.out.println (Arrays.toString(OneRow));
} // end of while
} // end of try
catch (Exception e) {
String errmsg = e.getMessage();
System.out.println ("File not found:" +errmsg);
} // end of Catch
return Rs;
}// end of ReadFile method
}// end of CSVFile class
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("T1Data");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
T1Data newContentPane = new T1Data();
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
class MyModel extends AbstractTableModel {
private String[] columnNames = { "1", "2", "3", "4", "5", "6"};
private ArrayList<String[]> Data = new ArrayList<>();
public void AddCSVData(ArrayList<String[]> DataIn) {
this.Data = DataIn;
this.fireTableDataChanged();
}
@Override
public int getColumnCount() {
return columnNames.length;//length;
}
@Override
public int getRowCount() {
return Data.size();
}
@Override
public String getColumnName(int col) {
return columnNames[col];
}
@Override
public Object getValueAt(int row, int col)
{
return Data.get(row)[col];
}
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
// T1Data.createAndShowGUI();
}
}
解决方法:
(我已经使用了您的命名对话,但您应该阅读我上面关于变量和类的Java命名约定的注释).
您创建一个表将新的MyModel传递给构造函数:
JTable Table = new JTable(new MyModel());
稍后您在actionPerformed()中创建一个(单独的)新MyModel:
MyModel NewModel = new MyModel();
File DataFile = fc.getSelectedFile();
ArrayList<String[]> Rs2 = Rd.ReadCSVfile(DataFile);
NewModel.AddCSVData(Rs2);
问题是,你永远不会调用table.setModel(NewModel).
要解决此问题,请将表变量存储为T1Data类中的字段,以便稍后引用它:
private final JTable table;
...
public T1Data() {
super(new BorderLayout(3,3));
this.table = new JTable(new MyModel());
this.table.setPreferredScrollableViewportSize(new Dimension(700, 70));
this.table.setFillsViewportHeight(true);
并在actionPerformed()中设置模型:
this.table.setModel(NewModel);
内容总结
以上是互联网集市为您收集整理的java – 从CSV文件读取数据并将其显示在JTable中全部内容,希望文章能够帮你解决java – 从CSV文件读取数据并将其显示在JTable中所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。