我正在尝试将从表单读入的数据添加到jTable中。到目前为止,这就是我所拥有的,我不知道为什么它不起作用。这是代码:
public void fillTable(){
String inputField1 = jTextArea1.getText();
String inputField2 = jTextField8.getText();
String inputField3 = jComboBox1.getSelectedItem().toString();
String inputField4 = jTextField11.getText();
DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
int numRows = jTable2.getRowCount();
for (int i = 0; i <= numRows; i++){
model.setValueAt(inputField1, numRows, 1);
model.setValueAt(inputField2, numRows, 2);
model.setValueAt(inputField3, numRows, 4);
model.setValueAt(inputField4, numRows, 6);
}
jTable2.setModel(model);
}
我得到的错误是:
Exception in thread "AWT-EventQueue=0" java.lang.ClassCastException: my.rcs.accounting.DraftInvoice$5 cannot be cast to groovy.model.DefaultTableModel
我做错了什么,如何解决这个问题?
谢谢!
答案 0 :(得分:2)
应该是i
而不是numRows
。
for (int i = 0; i <= numRows; i++) {
model.setValueAt(inputField1, i, 1);
model.setValueAt(inputField2, i, 2);
model.setValueAt(inputField3, i, 4);
model.setValueAt(inputField4, i, 6);
}
答案 1 :(得分:1)
您能找到创建您的jTable2的代码吗? 还有你用于DefaultTableModel的确切import(=包名)?
我怀疑ClassCastException可能来自这一行: DefaultTableModel model =(DefaultTableModel)jTable2.getModel();
引出两个问题:
1) what table model was initially associated with jTable2, it seems to be some inner class my.rcs.accounting.DraftInvoice$5 and the question is - does it inherit from DefaultTableModel
2) What DefaultTableModel are you expecting, naiively I'd expect it to be javax.swing.table.DefaultTableModel