0

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!

3
  • Where does the exception being thrown ? Also, it look weird that you are looping over each row of the table but only setting the row at numRows (which will throw an exception BTW). Commented Nov 24, 2014 at 10:39
  • What do you mean by where is the exception being thrown? This method is called when a button is clicked. I still get the same exception even if I don't use the for loop. Commented Nov 24, 2014 at 10:44
  • I mean, which line on your code. My guess is that jTable2.getModel return a my.rcs.accounting.DraftInvoice and not a groovy.model.DefaultTableModel which result in an exception when you try to cast DefaultTableModel model = (DefaultTableModel) jTable2.getModel();. You should try to use DraftInvoice model = (DraftInvoice ) jTable2.getModel(). Commented Nov 24, 2014 at 10:54

2 Answers 2

2

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);      
}
Sign up to request clarification or add additional context in comments.

Comments

1

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 

3 Comments

jTable2.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { }, new String [] { "Description", "Date", "Qty", "Rate", "VAT", "Total" } )
^^That is the model used when creating the jTable. This is the import code: import groovy.model.DefaultTableModel;
So you should import " javax.swing.table.DefaultTableModel", not that groovy stuff...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.