如何使用JFileChooser将CSV格式数据输入到JTable中
问题描述:
我目前正试图让我的程序将用户定义的CSV文件中的数据输入到JTable中。但是,由于某些原因,它无法正常工作,并且每当我尝试加载或保存文件时,NetBeans都会在调试器中抛出一些异常。如何使用JFileChooser将CSV格式数据输入到JTable中
private void openActionPerformed(java.awt.event.ActionEvent evt)
{
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
// What to do with the file, e.g. display it in a TextArea
CSVReader reader = new CSVReader(new FileReader(file.getAbsolutePath()));
List myEntries = reader.readAll();
table (myEntries.toArray());
} catch (IOException ex) {
System.out.println("problem accessing file"+file.getAbsolutePath());
}
} else {
System.out.println("File access cancelled by user.");
}
}
这是我对特定问题的代码。
如何正确使用此功能?
答
只有建议
你可以把Vector or Object[]到JTable直接
为
java.util.List
你要实现自定义AbstractTableModel,因为JTable
其XxxTableModel
基于过早Vector or Object[]
必须在上完成对
XxxTableModel
的所有更新为更好地帮助越早张贴SSCCE,其中从
CSV File
含量研究应该被硬编码到java.util.List
直接为
JTable
(视图)的所有数据都存储到XxxTableModel
(模型)