-1

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());
}
Carol.Kar
  • 4,581
  • 36
  • 131
  • 264

1 Answers1

1

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thxfor your answer! I understand the message. What I do not know is a way to remove my ResultTableModel? Hence, I tried the DefaultTableModel approach, which does not work. Any recommendations for a solution to get to delete the ResultTableModel? – Carol.Kar Nov 25 '14 at 13:20
  • 1
    Why do you mean by "delete the ResultTableModel"? Do you mean "clear it", i.e. make it so that it doesn't contain any row anymore? If so, your model gets its data from a list, isn't it? So if the list is cleared, so is the model, isn't it? So add a clear() method to your model, that clears the list it uses, and fire the appropriate event. – JB Nizet Nov 25 '14 at 13:23
  • Thx a lot for your answer! By event you mean `fireTableDataChanged();`? – Carol.Kar Nov 25 '14 at 13:27
  • 1
    Read the javadoc of AbstractTableModel, and find the method that you think the most appropriate to signal that rows have been removed: https://docs.oracle.com/javase/7/docs/api/javax/swing/table/AbstractTableModel.html#fireTableDataChanged%28%29 – JB Nizet Nov 25 '14 at 13:28
  • Thx for your previous answers! Please update your answer with a possible solution approach, so that I can accept it. – Carol.Kar Nov 25 '14 at 13:35
  • 1
    Nope. My answer and my comments have everything you need to solve the problem. I won't provide you code that you can copy and paste without understanding as you seem to have done until now. If you still don't understand something, feel free to ask. – JB Nizet Nov 25 '14 at 13:36
  • ;-). Thx for your reply! Please have a look at my update then. – Carol.Kar Nov 25 '14 at 13:39
  • 1
    First of all, List has a clear() method. You don't need any loop. Second, if you compute the row count to send in the event *after* you have removed all the rows, then you will obviously get 0. – JB Nizet Nov 25 '14 at 13:43
  • Thx for walking me through. I updated in update 2 my solution. What do you think about it? Would you do it like that? – Carol.Kar Nov 25 '14 at 13:47
  • 1
    No. Re-read my comment: if you compute the row count to send in the event after you have removed all the rows, then you will obviously get 0. You must fire an event containing the numbers of rows that have been deleted. getRowCount() will return 0 instead of the number of deleted rows, since you call it *after* the rows have been deleted. You must call it *before*, then delete the rows, then fire the event. – JB Nizet Nov 25 '14 at 13:49
  • So basically like that: ` public void clear() { int rowCount = getRowCount(); resultList.clear(); fireTableRowsDeleted(0, rowCount); }` – Carol.Kar Nov 25 '14 at 14:01
  • You got it. Congrats. – JB Nizet Nov 25 '14 at 14:54