我的项目涉及一个带有自定义TableModel的JTable。由于我希望用户能够修改表中的每个单元格,因此我选择让我的自定义TableModel(CSVModel)扩展DefaultTableModel。但是,由于某种原因,当我尝试编辑表格中的单元格时,CSVModel会抛出异常。我唯一的提示来自堆栈跟踪的最后一行:
线程中的异常“AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException:3> = 0 at
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&gt; = 0 at java.util.Vector.elementAt(Unknown Source) 在javax.swing.table.DefaultTableModel.setValueAt(未知来源) 在javax.swing.JTable.setValueAt(未知来源) 在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(未知来源) at javax.swing.JTextField $ NotifyAction.actionPerformed(Unknown Source) 在javax.swing.SwingUtilities.notifyAction(未知来源) 在javax.swing.JComponent.processKeyBinding(未知来源) 在javax.swing.JComponent.processKeyBindings(未知来源) 在javax.swing.JComponent.processKeyEvent(未知来源) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source) 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) 在java.awt.EventQueue.access $ 200(未知来源) 在java.awt.EventQueue $ 3.run(未知来源) 在java.awt.EventQueue $ 3.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source) 在java.awt.EventQueue $ 4.run(未知来源) 在java.awt.EventQueue $ 4.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 在java.awt.EventDispatchThread.run(未知来源)
感谢您的帮助!
答案 0 :(得分:0)
解决方案是扩展AbstractTableModel而不是DefaultTableModel。我还必须包含AbstractTableModel的两个方法的适当重载版本:
@Override public boolean isCellEditable(int row, int col) { return true; }
@Override
public void setValueAt(Object value, int row, int col) {
Data.get(row)[col] = (String) value;
fireTableCellUpdated(row, col);
}