java 用 CSV 数据填充 JTable 的简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1420310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easy way to fill a JTable with CSV data
提问by Romain Linsolas
I have CSV data (in fact the values are separated by a tab character, but I can use ;as the separator). The data are stored in a Stringobject.
我有 CSV 数据(实际上这些值由制表符分隔,但我可以;用作分隔符)。数据存储在一个String对象中。
Is there simple way to create a JTable with this data (without doing it manually by reading and parsing the Stringobject) ?
是否有使用这些数据创建 JTable 的简单方法(无需通过读取和解析String对象手动创建)?
(note: my project is using Java 1.4, but if you have a solution that needs Java 1.5, I would be happy anyway)
(注意:我的项目使用的是 Java 1.4,但如果您有需要 Java 1.5 的解决方案,我会很高兴)
采纳答案by Pete Kirkham
The TableModelExtTextLoaderfrom swinglabswill do you. It supports both tab and comma separated text.
该TableModelExtTextLoader从的SwingLabs会做你。它支持制表符和逗号分隔的文本。
回答by Thorn
It's easy enough to do this without an external library. Here is an example using the Scanner class from Java 1.5:
在没有外部库的情况下很容易做到这一点。这是使用 Java 1.5 中的 Scanner 类的示例:
import java.io.*;
import java.net.URL;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class CSVTable extends JFrame {
JTable table;
DefaultTableModel model;
JButton closeButton, webButton;
/**
* Takes data from a CSV file and places it into a table for display.
* @param source - a reference to the file where the CSV data is located.
*/
public CSVTable(String title, String source) {
super(title);
table = new JTable();
JScrollPane scroll = new JScrollPane(table);
String[] colNames = { "LastName", "FirstName", "Email Address", "Dept."};
model = new DefaultTableModel(colNames, 0);
InputStream is;
try {
if(source.indexOf("http")==0) {
URL facultyURL = new URL(source);
is = facultyURL.openStream();
}
else { //local file?
File f = new File(source);
is = new FileInputStream(f);
}
insertData(is);
//table.getColumnModel().getColumn(0).setCellRenderer(new CustomCellRenderer());
}
catch(IOException ioe) {
JOptionPane.showMessageDialog(this, ioe, "Error reading data", JOptionPane.ERROR_MESSAGE);
}
JPanel buttonPanel = new JPanel();
closeButton = new JButton("Close");
webButton = new JButton("Proctinator.com");
buttonPanel.add(closeButton);
buttonPanel.add(new JLabel(" You can download this file from our site: "));
buttonPanel.add(webButton);
JPanel notesPanel = new JPanel();
JLabel note1 = new JLabel(" Make sure that your list is formatted exactly as shown below, including the *markers between categories ");
JLabel note2 = new JLabel(" Be sure to place each faculty member into the correct category: *Teacher, *Subs, *TeacherAids, *TeacherAssistants ");
JLabel note3 = new JLabel(" Note that the your faculty list must be a plain text file: Export to either CSV or tab delimited format.");
BoxLayout layout = new BoxLayout(notesPanel, BoxLayout.Y_AXIS);
notesPanel.setLayout(layout);
notesPanel.add(note1);
notesPanel.add(note2);
notesPanel.add(note3);
getContentPane().add(notesPanel, BorderLayout.NORTH);
getContentPane().add(scroll, BorderLayout.CENTER);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
pack();
}
/**
* Places the data from the specified stream into this table for display. The data from the file must be in CSV format
* @param is - an input stream which could be from a file or a network connection or URL.
*/
void insertData(InputStream is) {
Scanner scan = new Scanner(is);
String[] array;
while (scan.hasNextLine()) {
String line = scan.nextLine();
if(line.indexOf(",")>-1)
array = line.split(",");
else
array = line.split("\t");
Object[] data = new Object[array.length];
for (int i = 0; i < array.length; i++)
data[i] = array[i];
model.addRow(data);
}
table.setModel(model);
}
public static void main(String args[]) {
CSVTable frame = new CSVTable("Faculty List Example","http://proctinator.com/help/faculty.csv");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}
回答by prhayes
unless there is a csv -> tablemodel method out there somewhere you will have to write the code to populate the model yourself unfortunately.
除非在某处有 csv -> tablemodel 方法,否则不幸的是,您将不得不编写代码来自己填充模型。

