删除 jtable 中未使用的行(空行)?

remove unused rows in jtable (Empty Rows)?

关于删除 jtable 中未使用的行的问题 我正在使用 DefualtTableModel 我的 table 已经有一些数据 & 当我更新它时将一些列留空以便稍后更新主题所以他们是空列..我想在保存数据之前用按钮删除主题..我实际上试过这个代码:

private void btn_ClearActionPerformed(java.awt.event.ActionEvent evt) {                                              
      table.setAutoCreateRowSorter(true); 

      TableRowSorter sorter = (TableRowSorter) table.getRowSorter();
      sorter.setRowFilter(new RowFilterImpl());                
}

我也试过这个:

private void btn_ClearActionPerformed(java.awt.event.ActionEvent evt) {
    table.setAutoCreateRowSorter(true);
    TableRowSorter sorter = (TableRowSorter) table.getRowSorter();
    sorter.setRowFilter(new RowFilter<TableModel, Integer>() {
        @Override
        public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
            boolean included = true;
            Object cellValue = entry.getModel().getValueAt(entry.getIdentifier(), 0);
            if (cellValue == null || cellValue.toString().trim().isEmpty()) {
                included = false;
            }
            return included;
        }
    });
}

上面的代码有效,但我不喜欢它,因为它会在过滤后调整行的大小,所以我想使用 if 条件对 model.remove(); 做一些事情..我想指定列例如第 7 列和第 12 列,并且只想删除指定列中的空行..

好的,我试过这个代码:

for (int i = model.getRowCount() - 1; i >= 0; i--)
{
     Object col1 = model.getValueAt( i,model.getColumnCount() - 6);
     Object col2 = model.getValueAt( i,model.getColumnCount() - 11);

     if (col1 == null || col2 == null)
         model.removeRow(i);
}

我遇到了同样的问题,我发现下面的这段代码引起了那个问题,所以我删除了它……我还发现它会计算你选择或点击一行的次数,然后根据你点击的次数调整它的大小!

table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
       int lastRow = -1;

       public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
               if (lastRow != -1) {
                  table.setRowHeight(lastRow, table.getRowHeight());
              }

              int row = table.getSelectedRow();
              table.setRowHeight(row, 23);
              lastRow = row;
         }
       }
   });

有什么想法吗?

提前致谢

创建一个循环以从模型中删除数据。

可能是这样的:

for (int i = model.getRowCount() - 1; i >= 0; i--)
{
    if (column?? == null && column?? == null)
        model.removeRow(i);
}

added the problem above in table.setRowHeight();

嗯,这应该是原始问题的一部分。我们怎么知道你有自定义逻辑在做一些奇怪的事情???

将来 post 一个适当的 SSCCE 来演示问题,这样我们就不必猜测你在做什么。

i get the same problem it resizes rows

然后移除监听器:

  1. 删除侦听器
  2. 删除行
  3. 添加监听器