Reputation: 107
I am trying to add data read in from a form into a jTable. So far this is what I have and Im not sure why it will not work. This is the code:
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);
}
The error I get is:
Exception in thread "AWT-EventQueue=0" java.lang.ClassCastException: my.rcs.accounting.DraftInvoice$5 cannot be cast to groovy.model.DefaultTableModel
What am I doing wrong and how can I fix this?
Thank you!
Upvotes: 0
Views: 1137
Reputation: 244
It's should be i
instead of 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);
}
Upvotes: 2
Reputation: 2381
Could you locate the code that creates your jTable2? And also the exact import (=package name) you use for DefaultTableModel ?
I suspect the ClassCastException could come from this line: DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
which begs two questions:
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
Upvotes: 1