我的項目涉及一個帶有自定義TableModel的JTable。由於我希望用戶能夠修改表格中的每個單元格,我選擇讓我的自定義TableModel(CSVModel)擴展DefaultTableModel。但是,由於某種原因,當我嘗試編輯表格中的單元格時,CSVModel會引發異常。我唯一的提示是從堆棧跟蹤的最後一行:單元格的修改,JTable
在異常線程「AWT-EventQueue的-0」 java.lang.ArrayIndexOutOfBoundsException:3> = 0在
3是的行數我試圖修改的單元格。所以也許我的數據出問題了?下面是代碼:
class CSVModel extends DefaultTableModel {
private String[] columnNames;
private ArrayList<String[]> Data = new ArrayList<String[]>();
public void AddCSVData(ArrayList<String[]> DataIn) {
this.columnNames = DataIn.get(0);
DataIn.remove(0);
this.Data=DataIn;
this.fireTableStructureChanged();
}
@Override
public int getColumnCount() {
if (columnNames != null)
return columnNames.length;//length;
else
return 0;
}
@Override
public int getRowCount() {
if (Data != null)
return Data.size();
else
return 0;
}
public void setRowCount() {
Data=null;
columnNames=null;
this.fireTableStructureChanged();
}
@Override
public String getColumnName(int col) {
System.out.println("getcol");
return columnNames[col];
}
@Override
public Object getValueAt(int row, int col)
{
return Data.get(row)[col];
}
public ArrayList<String[]> ReadCSVFile(File dataFile) {
// When user presses load, read in the selected CSV file
CSVFile temp = new CSVFile();
return temp.ReadCSVfile(dataFile);
}
編輯之前,我打開一個CSV使用此類文件導入到表:
public class CSVFile {
private ArrayList<String[]> Rs = new ArrayList<>();
private String[] OneRow;
// Method for reading CSV file
public ArrayList<String[]> ReadCSVfile (File DataFile) {
try {
BufferedReader brd = new BufferedReader (new FileReader(DataFile));
String st = brd.readLine();
while (st != null) {
OneRow = st.split(",");
Rs.add(OneRow);
st = brd.readLine();
} // end of while
brd.close();
} // 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
完整的堆棧跟蹤:在線程
例外「AWT-EventQueue- 0「java.lang.ArrayIndexOutOfBoundsException:3> = 0 at java.util.Vector.elementAt(Unknown Source) at javax.swing.table.DefaultTableModel.setValueAt(Unknown Source) at javax.swing.JTable.setValueAt(未知的Sou RCE) 在javax.swing.JTable.editingStopped(來源不明) 在javax.swing.AbstractCellEditor.fireEditingStopped(來源不明) 在javax.swing.DefaultCellEditor中的$ EditorDelegate.stopCellEditing(來源不明) 在javax.swing.DefaultCellEditor中.stopCellEditing(來源不明) 在javax.swing.JTable中的$ GenericEditor.stopCellEditing(來源不明) 在javax.swing.DefaultCellEditor中的$ EditorDelegate.actionPerformed(來源不明) 在javax.swing.JTextField.fireActionPerformed(來源不明) 在javax.swing.JTextField.postActionEvent(來源不明) 在javax.swing.JTextField中的$ NotifyAction.actionPerformed(來源不明) 在javax.swing.SwingUtilities.notifyAction(來源不明) (未知源) at javax.swing.JComponent.processKeyEvent(Unknown Source) at java.awt.Component.processEvent(Unknown) at javax.swing.JComponent.processKeyBinding源) 在java.awt.Container.processEvent(來源不明) 在java.awt.Component.dispatchEventImpl(來源不明) 在java.awt.Container.dispatchEventImpl(來源不明) 在java.awt.Component.dispatchEvent (Unknown Source) at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.preDispatchKey事件(未知源) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt。 Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java。 (Native Source) at java.awt.EventQueue $ 3.run(未知源) ethod) at java.security.ProtectionDomain $ 1。doIntersectionPrivilege(來源不明) 在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(來源不明) 在java.awt.EventQueue中的$ 4.run(來源不明) 在java.awt.EventQueue中的$ 4.run(來源不明) 是java .security.AccessController.doPrivileged(本機方法) 在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(來源不明) 在java.awt.EventQueue.dispatchEvent(來源不明) 在java.awt.EventDispatchThread.pumpOneEventForFilters(來源不明) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 在java.awt.EventDispatchThread.pumpEvents(來源不明) 在java.awt.EventDispatchThread.run(來源不明)
感謝您的幫助!