2012-08-17 79 views
2

我目前正试图让我的程序将用户定义的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."); 
    } 
} 

这是我对特定问题的代码。

如何正确使用此功能?

回答

3

只有建议

  1. 你可以把Vector or Object[]JTable直接

  2. java.util.List你要实现自定义AbstractTableModel,因为JTableXxxTableModel基于过早Vector or Object[]

  3. 必须在上完成对XxxTableModel的所有更新

  4. 为更好地帮助越早张贴SSCCE,其中从CSV File含量研究应该被硬编码到java.util.List直接

  5. JTable(视图)的所有数据都存储到XxxTableModel(模型)

相关问题