[英]JTable does not remove row
I need to remove rows form a JTable
. 我需要从JTable
删除行。 I wrote the code like this: 我写了这样的代码:
DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
int x = 0;
int row = dtm.getRowCount();
while(row>=x){
dtm.removeRow(x);
x++;
}
But it generates an error, like: 但这会产生错误,例如:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException:
24 >= 24
A simpler solution is to use: 一个更简单的解决方案是使用:
dtm.setRowCount(0);
This is also more efficient since the table only needs to repaint itself once, after all the rows have been deleted. 这也更加有效,因为在删除所有行之后,该表仅需要自我重绘一次。
You can try this: 您可以尝试以下方法:
while(row>x){
dtm.removeRow(x);
x++;
}
UPDATE 更新
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
model.addColumn("Col1");
model.addColumn("Col2");
model.addRow(new Object[]{"1", "2"});
model.addRow(new Object[]{"1", "2"});
table.setModel(model);
for(int index = 0; index<table.getRowCount();)
{
model.removeRow(index);
}
With Java installed it is easier. 安装Java后,操作更加简单。 Sorry about the wrong answer. 对不起,答案错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.