1

I have a JTable. This table is using a custom model that I designed; the custom model is extends AbstractTableModel. I have a button which enables a user to delete a selected/highlighted row.

I have tried this code but its giving me a class cast exception -

myTableModel cannot be cast to DefaultTableModel. 

Below is the code.

DefaultTableModel model =  (DefaultTableModel)table.getModel();
        model.removeRow(table.convertRowIndexToModel(table.getSelectedRow()));
        model.fireTableDataChanged();`

I have searched the web but it is always DefaultTableModel - but I have AbstarctTableModel.

How do we solve this?

Vogel612
  • 5,620
  • 5
  • 48
  • 73
CN1002
  • 1,115
  • 3
  • 20
  • 40
  • Please show your custom model, if it's not too big. – Puce Jan 29 '15 at 15:08
  • 3
    method fireTableDataChanged is implemented in DefaultTableModel, then there isn't reason to call that twice, fireTableDataChanged reseting all custom methods (rendere and editor too), read AbstractTableModels API, there is method for remove row and with proper notifier not fireTableDataChanged – mKorbel Jan 29 '15 at 15:18
  • 3
    *"I have searched the web but it is always `DefaultTableModel` - but I have `AbstarctTableModel`."* There's a good reason for that, of which this is an example. The `DefaultTableModel` has this functionality implemented (and without known bugs). But for better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Jan 29 '15 at 15:33
  • 1
    I took the liberty to roll back your extensive edits after your problem had been solved. Please don't invalidate existing answers – Vogel612 Jan 30 '15 at 07:29

1 Answers1

6

I have tried this code but its giving me a class cast exception - myTableModel cannot be cast to DefaultTableModel.

The error is pretty self-explanatory: given you provide the table with your own table model, then table.getModel() won't ever return a DefaultTableModel instance.

How do we solve this?

By down-casting table.getModel() to the appropriate class (your class). Then call the method you provided to remove a row from your table model. For example:

int modelRowIndex = table.convertRowIndexToModel(table.getSelectedRow());
MyTableModel model = (MyTableModel)table.getModel();
model.removeRowFromMyModel(modelRowIndex);

See a complete example of a custom table model extending from AbstractTableModel in this question.

Off-topic

We never should call any of fireXxx() methods explicitely from the outside. Those are intended to be called internally by AbstractTableModel subclasses when needed. IMHO those should be protected and not public, to avoid use them incorrectly. But for some reason I'm not aware of they made them public.

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • 2
    1+ `. IMHO those should be protected and not public, to avoid use them incorrectly.` good point, that would make our lives easier ;) – camickr Jan 29 '15 at 15:50
  • @dic 19 I have followed the model you have shown me, but its giving me an error as shown in the edited question. How do we resolve this error? – CN1002 Jan 30 '15 at 07:09
  • Correct me if I'm wrong, but even with downcasting, you won't be able to solve the problem because you wouldn't still have access to the method `removeRow(int rowIndex)` from `DefaultTableModel`. Are you suggesting that he implements his own method to remove the desired row? – Jesse James Apr 16 '20 at 12:12
  • @JesseJames Hi there, yes it is actually stated by the OP: *This table is using a custom model that I designed; the custom model is extends AbstractTableModel* – dic19 Apr 16 '20 at 15:15