我试图将从表单读取的数据添加到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


我在做什么错,我该如何解决?

谢谢!

最佳答案

您能找到创建jTable2的代码吗?
还有用于DefaultTableModel的确切导入(=包名称)?

我怀疑ClassCastException可能来自此行:
DefaultTableModel模型=(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

10-07 19:01