3

I would like to remove selected row from JTable with AbstractTableModel using a button.

The code below works with DefaultTableModel:

...
MyTableModel mtb;
...
private String[]....
private Object[][]...
...
JTable table = new JTable(mtb)
JButton delete;
...
 public void actionPerformed(ActionEvent e) {

        if(e.getSource().equals(delete))
         {
                 if(table.getSelectedRow()<0)
                 {
                  JOptionPane.showMessageDialog(this,"Select row");

                 }
                 else
                 {
                     mtb.removeRow(table.getSelectedRow()); 

                 }
         }
     }

but it deosn't work with AbstractTablemodel.

I have a little mess in my code so here is java example from oracle page that can be used:

Thanks!

TheSpaceboy0
  • 95
  • 1
  • 2
  • 8
  • 2
    It does not work since that method is not defined. Just add such a method to your `TableModel` and make sure that method fires the proper events. Note that the code in your question is rather irrelevant for proposing a solution. We would need your `TableModel` iso an `actionPerformed` method to give useful advise – Robin Dec 14 '12 at 14:31
  • OK, I thought that there is a similar defined method as it is in DefaultTableModel but now I know I need to define it. Thanks. – TheSpaceboy0 Dec 14 '12 at 15:06

2 Answers2

5

For AbstractTableModel, you have to implement your own removeRow() based on your model's internal data structure(s), but you can study the source of DefaultTableModel as a guide on which event(s) to fire. For example,

public void removeRow(int row) {
    // remove a row from your internal data structure
    fireTableRowsDeleted(row, row);
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I've found more relevant tip: "It's not easy to add data to an existing array, you have to create a new array bigger than the old one, copy all the data across, and then add the new item. Far better to use a Vector or an ArrayList." – TheSpaceboy0 Dec 14 '12 at 14:58
  • 2
    *I've found more relevant tip* our magic crystal ball probably failed to reveal you were using an array as internal data structure. +1 for this answer which is about the best one could do with the available info in the question – Robin Dec 14 '12 at 16:23
  • Although not deprecated, `Vector` is a legacy class. Consider coding to the interface, e.g. `List`, which lets you change implementations as required – trashgod Dec 14 '12 at 20:34
3

DefaultTableModel will itself call fireXX methods whenever there is a change in the table model. But if we use AbstractTableModel then we have to explicitly call fireXX methods. So when ever there is a change in the table just call relevant fireXX method.

For,

inserting a new row to the table use fireTableRowsInserted

deletion (in your case) use fireTableRowsDeleted

update use fireTableRowsUpdated

NOTE: DefaultTableModel has most of all the methods implemented. So unless there is a real need go for AbstractTableModel else stick with DefaultTableModel.

Amarnath
  • 8,736
  • 10
  • 54
  • 81