关于删除jtable
中未使用的行的问题我正在使用DefualtTableModel
我的表已经有一些数据并且当我更新它时将一些列留空以稍后更新主题,以便它们是空列..我想在保存数据之前用按钮删除主题..我实际上尝试了这段代码:
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();
嗯,这应该是原始问题的一部分。我们怎么知道你有自定义逻辑在做一些奇怪的事情???
将来发布适当的SSCCE这说明了问题,因此我们不必猜测您在做什么。
i get the same problem it resizes rows
然后删除监听器:
- 删除监听器
- 删除行
- 添加监听器
关于java - 删除 jtable 中未使用的行(空行)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35139916/