使用AbstractTableModel从JTable中删除或添加行

时间:2022-01-25 04:22:58

ive been trying to update the table after the insertion or deletion of items from a abstract table model but whenever i do that, instead of removing the old records and replace with the new ones, the old rows remains and it creates all the rows again without removing the old ones..so i get duplicate items, this is the code im using : for the data inserted :

我一直想更新后的表插入或删除的物品从一个抽象的表模型,但每当我这样做,而不是删除旧的记录和替换为新的,旧行仍然再它创建的所有行没有删除旧的. .我得到重复的项目,这是im使用的代码:对于插入的数据:

TestModel tm = new TestModel() ;

                    tm.fireTableRowsInserted(records.length, records.length);

and for the data deleted :

对于已删除的数据:

TestModel tm = new TestModel() ;
                    tm.fireTableRowsDeleted(records.length, records.length);

any clue of how to get around with that? any help is greatly appreciated! Kind regards, Romulo Romero

你知道怎么处理吗?非常感谢您的帮助!亲切的问候,罗慕洛罗梅罗

1 个解决方案

#1


1  

Create a table with a boolean column. Since using this boolean column you can delete those rows that are selected for deletion. Just like the following screen shot,

创建一个包含布尔列的表。由于使用这个布尔列,您可以删除那些被选择删除的行。就像下面的截图一样,

使用AbstractTableModel从JTable中删除或添加行

Then in your TableModel make a List<StudentDO> such that it will hold all the table data.

然后在TableModel中创建一个列表 ,使其包含所有表数据。

Adding a Row:

添加一行:

To add a row just create a new StudentDO and send it to the table model and the model addRow method will add the object to the table list.

要添加一行,只需创建一个新学生,并将其发送到表模型,模型addRow方法将把对象添加到表列表中。

Deleting Rows:

删除行:

For Deleting rows just call a delete method and this should fire event in TableModel such that model should traverse all the rows and check which ever row is selected and delete it.

对于删除行,只需调用delete方法,它应该在TableModel中触发事件,这样模型应该遍历所有的行并检查选择了哪个行并删除它。

Note: Deleting rows should be done from the end not from the beginning of the list.

注意:应该从末尾删除行,而不是从列表的开头。

StudentTableModel.java

StudentTableModel.java

class StudentTableModel {

    // Required methods code goes here.  

    public void addRow(StudentDO do1) {
        data.add(do1);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void deleteRow() {
           for(int rowIndex = data.size() - 1; rowIndex >= 0; rowIndex--) {
            if(data.get(rowIndex).isSelect()) {
          data.remove(rowIndex);
         }
           }
     fireTableDataChanged();
    }
}

P.S: fireXXXMethods should be called only in the model. Because any data change will be responsible of the model.

P。S:应该只在模型中调用fireXXXMethods。因为任何数据更改都将对模型负责。

#1


1  

Create a table with a boolean column. Since using this boolean column you can delete those rows that are selected for deletion. Just like the following screen shot,

创建一个包含布尔列的表。由于使用这个布尔列,您可以删除那些被选择删除的行。就像下面的截图一样,

使用AbstractTableModel从JTable中删除或添加行

Then in your TableModel make a List<StudentDO> such that it will hold all the table data.

然后在TableModel中创建一个列表 ,使其包含所有表数据。

Adding a Row:

添加一行:

To add a row just create a new StudentDO and send it to the table model and the model addRow method will add the object to the table list.

要添加一行,只需创建一个新学生,并将其发送到表模型,模型addRow方法将把对象添加到表列表中。

Deleting Rows:

删除行:

For Deleting rows just call a delete method and this should fire event in TableModel such that model should traverse all the rows and check which ever row is selected and delete it.

对于删除行,只需调用delete方法,它应该在TableModel中触发事件,这样模型应该遍历所有的行并检查选择了哪个行并删除它。

Note: Deleting rows should be done from the end not from the beginning of the list.

注意:应该从末尾删除行,而不是从列表的开头。

StudentTableModel.java

StudentTableModel.java

class StudentTableModel {

    // Required methods code goes here.  

    public void addRow(StudentDO do1) {
        data.add(do1);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void deleteRow() {
           for(int rowIndex = data.size() - 1; rowIndex >= 0; rowIndex--) {
            if(data.get(rowIndex).isSelect()) {
          data.remove(rowIndex);
         }
           }
     fireTableDataChanged();
    }
}

P.S: fireXXXMethods should be called only in the model. Because any data change will be responsible of the model.

P。S:应该只在模型中调用fireXXXMethods。因为任何数据更改都将对模型负责。