javaswingjtabletablemodelabstracttablemodel

TableModel cannot be cast to javax.swing.table.DefaultTableModel


I want to clear my custom TableModel and get the following exception:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.Test.gui.results.ResultTableModel cannot be cast to javax.swing.table.DefaultTableModel

I want to clear my model like that:

                DefaultTableModel model = (DefaultTableModel) resultTable.getModel();
                model.setRowCount(0);

That is my ResultsTableModel:

public class ResultTableModel extends AbstractTableModel {

    /**
     * UUID
     */
    private static final long serialVersionUID = -8928042813952799089L;

    private String[] columnNames = {"Customer", "Test1"};


    private List<TestData> resultList = new ArrayList<TestData>();

    private TestData rs;


    public ResultTableModel(List<TestData> resultList){
        this.resultList=resultList;
    }

    @Override
    public int getRowCount() {
        return resultList.size();
    }

    /**
     * get Value at
     */
    public Object getValueAt(int row, int col) {
        TestData r = resultList.get(row);
        switch (col) {
        case 0:
            return r.getCustomer();
        case 1:
            return r.getEquity();
        case 2:
            return r.getCyclicalRiskMarketAvg(); 
        case 3:
            return r.getDscr_1();
        case 4:
            return r.getDscr_2();
        case 5:
            return r.getDscr_3();
        case 6:
            return r.getDscr_4();
        case 7:
            return r.getDscr_5();
        case 8:
            return r.getDscr_6();
        case 9:
            return r.getDscr_7();
        case 10:
            return r.getDscr_8();
        case 11:
            return r.getDscr_9();
        case 12:
            return r.getDscr_10();
        case 13:
            return r.getLtv_1();
        case 14:
            return r.getLtv_2();
        case 15:
            return r.getLtv_3();
        case 16:
            return r.getLtv_4();
        case 17:
            return r.getLtv_5();
        case 18:
            return r.getLtv_6();
        case 19:
            return r.getLtv_7();
        case 20:
            return r.getLtv_8();
        case 21:
            return r.getLtv_9();
        case 22:
            return r.getLtv_10();
        default:
            break;
        }
        fireTableDataChanged();
        return null;
    }

    public void setTestData(TestData rd){
        resultList.add(rd);
        fireTableRowsInserted(resultList.size()-1, resultList.size()-1);
    }

    @Override 
    public String getColumnName(int index) { 
        return columnNames[index]; 
    } 

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    /**
     * @return the columnNames
     */
    public String[] getColumnNames() {
        return columnNames;
    }

    /**
     * @param columnNames the columnNames to set
     */
    public void setColumnNames(String[] columnNames) {
        this.columnNames = columnNames;
    }

    /**
     * checks if the cells are editable 
     */
    public boolean isCellEditable(int row, int col) { 
        return false; 
    }

    /**
     * @return the resultList
     */
    public List<TestData> getResultList() {
        return resultList;
    }

    /**
     * @param resultList the resultList to set
     */
    public void setResultList(List<TestData> resultList) {
        this.resultList = resultList;
    }

    /**
     * 
     * @return
     */
    public TestData getRs() {
        return rs;
    }

    /**
     * @param rs the rs to set
     */
    public void setRs(TestData rs) {
        this.rs = rs;
    }

}

I understand that my cast does not work. However, I would like to clear my model.

Any recommendation how to clear my model?

I appreciate your answer!

UPDATE

I added this method to my TableModel:

public void clear() {
    for (int i = 0; i < resultList.size(); i++) {
        resultList.remove(i);
    }
    fireTableRowsDeleted(0, getRowCount());
}

However, now when I click my button only some rows get deleted, which looks extremely random to me.

UPDATE 2

This solution works:

public void clear() {
    resultList.clear();
    fireTableRowsDeleted(0, getRowCount());
}

Solution

  • The message is extremely clear. Your table has a model of type ResultTableModel, which extends AbstractTableModel. You get the model, and cast it to DefaultTableModel. But it's not a DefaultTableModel. It's a ResultTableModel. So you get an exception.

    That's like taking a bicycle and pretend it's a plane. It's not a plane, so there's no way you can make it fly.