remove rows from JTable with AbstractTableModel

前端 未结 2 632
遇见更好的自我
遇见更好的自我 2020-12-11 22:43

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

The code below works with DefaultTableModel:

<
相关标签:
2条回答
  • 2020-12-11 23:18

    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);
    }
    
    0 讨论(0)
  • 2020-12-11 23:43

    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.

    0 讨论(0)
提交回复
热议问题