2

我在班级级别有一个 JTable 和 DefaultTableModel。第一次初始化 jTable 和模型时,我添加了 10 行。在按钮上单击我从我的数据库中得到 3 行,所以我必须通过删除前面的 10 行来仅显示这 3 行。我这样做是使用tableModel.setDataVector(data, columnNames);
但主要问题是,该表仅用新行替换了 3 行旧行,而其余 7 行仍按原样显示。

任何人都可以帮忙吗?

谢谢,杰

4

1 回答 1

1

如果我理解了,您想用数据库中的 3 行重建表并删除前面的 10 行。如果这是真的,那么这样做,

 public void actionPerformed(ActionEvent e) {
   // If the action is for getting data from DB then do,
   DefaultTableModel dtm = (DefaultTableModel) myTable.getModel();
   // int rowCount = dtm.getRowCount();
   /*
     for (int row = rowCount - 1; row >= 0; row--) {
        dtm.removeRow(row); 
   } */
   // or
   if(dtm.getRowCount() > 0) {
     dtm.setRowCount(0);
   }
// Add code to get your 3 rows from the database.
}
于 2012-08-24T20:01:27.580 回答