Java 用CSV数据填充JTable的简单方法
我有CSV数据(事实上,这些值由一个制表符分隔,但我可以使用Java 用CSV数据填充JTable的简单方法,java,swing,csv,jtable,Java,Swing,Csv,Jtable,我有CSV数据(事实上,这些值由一个制表符分隔,但我可以使用;作为分隔符)。数据存储在字符串对象中 有没有简单的方法可以使用这些数据创建JTable(无需通过读取和解析字符串对象手动执行) (注意:我的项目使用的是Java 1.4,但是如果您有一个需要Java 1.5的解决方案,我还是很高兴的)除非有一个csv->tablemodel方法,否则您将不得不自己编写代码来填充模型。很遗憾,from将为您提供帮助。它同时支持制表符和逗号分隔的文本。无需外部库即可轻松完成此操作。下面是一个使用Java
;
作为分隔符)。数据存储在字符串
对象中
有没有简单的方法可以使用这些数据创建JTable(无需通过读取和解析字符串
对象手动执行)
(注意:我的项目使用的是Java 1.4,但是如果您有一个需要Java 1.5的解决方案,我还是很高兴的)除非有一个csv->tablemodel方法,否则您将不得不自己编写代码来填充模型。很遗憾,from将为您提供帮助。它同时支持制表符和逗号分隔的文本。无需外部库即可轻松完成此操作。下面是一个使用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);
}
import java.io.*;
导入java.net.URL;
导入java.util.Scanner;
导入javax.swing.*;
导入javax.swing.table.DefaultTableCellRenderer;
导入javax.swing.table.DefaultTableModel;
公共类CSVTable扩展JFrame{
JTable表;
默认表格模型;
JButton closeButton,webButton;
/**
*从CSV文件中获取数据并将其放入表格中显示。
*@param source-对CSV数据所在文件的引用。
*/
公共CSVTable(字符串标题、字符串源){
超级(标题);
table=新的JTable();
JScrollPane scroll=新的JScrollPane(表);
字符串[]colNames={“LastName”、“FirstName”、“Email Address”、“Dept.”};
model=新的DefaultTableModel(colNames,0);
输入流为;
试一试{
if(source.indexOf(“http”)==0){
URL facultyURL=新URL(源);
is=facultyURL.openStream();
}
还有{//本地文件吗?
文件f=新文件(源);
is=新文件输入流(f);
}
插入数据(is);
//table.getColumnModel().getColumn(0.setCellRenderer)(新的CustomCellRenderer());
}
捕获(ioe异常ioe){
showMessageDialog(这个,ioe,“读取数据时出错”,JOptionPane.Error\u消息);
}
JPanel buttonPanel=新的JPanel();
closeButton=新按钮(“关闭”);
webButton=新的JButton(“Proctinator.com”);
按钮面板。添加(关闭按钮);
add(新的JLabel(“您可以从我们的网站下载此文件:”);
按钮面板。添加(webButton);
JPanel notesPanel=新的JPanel();
JLabel note1=新的JLabel(“确保列表的格式完全如下图所示,包括类别之间的*标记”);
JLabel note2=新JLabel(“确保将每位教员归入正确的类别:*教师、*替补、*教职人员、*助教”);
JLabel note3=新JLabel(“请注意,您的教员列表必须是纯文本文件:导出为CSV或制表符分隔格式。”);
BoxLayout layout=新的BoxLayout(注:BoxLayout.Y_轴);
注:设置布局(布局);
注:新增(注1);
注1.添加(注2);
注1.添加(注3);
getContentPane().add(notesPanel,BorderLayout.NORTH);
getContentPane().add(滚动,BorderLayout.CENTER);
getContentPane().add(buttonPanel,BorderLayout.SOUTH);
包装();
}
/**
*将指定流中的数据放入此表中显示。文件中的数据必须为CSV格式
*@param是一个输入流,可以来自文件、网络连接或URL。
*/
void insertData(InputStream为){
扫描仪扫描=新扫描仪(is);
字符串[]数组;
while(scan.hasNextLine()){
String line=scan.nextLine();
if(第indexOf行(“,”>-1)
数组=line.split(“,”);
其他的
数组=行。拆分(“\t”);
Object[]data=新对象[array.length];
for(int i=0;i
}这就是我要找的;)我已经实现了创建此表的手动代码,但我想看看是否有更简单的方法…感谢您的回答,但我的问题是如何在不重新发明轮子的情况下实现。从CSV文件中提取数据很容易,也可以将其插入JTable…OpenCSV也是获取数据的一种简单方法: