如何从 Jtable 中删除具有特定值的行?
How to remove rows with certain values from Jtable?
我有一个 Jtable,我从 .csv 文件中加载了值,但它会为文件中显示的每个 5/13/2013 实例创建一个新行,例如所以:
我想从 table 中删除包含此信息的所有行,但不确定如何操作。对我有什么建议吗?
这是我将数据添加到 table 的代码,如果有帮助的话:
while (inputStream.hasNext()) {
String data = inputStream.next();
String[] values = data.split(",");
tableModel.insertRow(tableModel.getRowCount(), values);
}//end of while block`
重申并完全清楚,我想从 table 中完全删除包含“5/13/2013”的每一行。顺便说一下,我使用的是默认 table 模型。
我最终应用了一个 for 循环和一个 if 语句来让它为我工作。
//remove rows with instances of "5/13/2013"
for (int i = 0; i < tableModel.getRowCount(); i++) {
if (((String)tableModel.getValueAt(i, 0)).equals("5/13/2013")) {
tableModel.removeRow(i);
}//end of if block
}//end of for block
它对我来说效果很好,并且已经摆脱了每一行。希望这可以帮助其他人。
while(i < tableModel.getRowCount()) {
//if the value at (i, 0) match the specified value the row will be removed
/*
if the row removed all row will move up and their index will be changed
so you have to add a condition if the value from the table doesn't match
the specified value the iterator i will iterate by one to jump to the next
row
*/
if (((String)tableModel.getValueAt(i, 0)).equals("5/13/2013")) {
tableModel.removeRow(i);
}else {
++i;
}
}
我有一个 Jtable,我从 .csv 文件中加载了值,但它会为文件中显示的每个 5/13/2013 实例创建一个新行,例如所以:
我想从 table 中删除包含此信息的所有行,但不确定如何操作。对我有什么建议吗?
这是我将数据添加到 table 的代码,如果有帮助的话:
while (inputStream.hasNext()) {
String data = inputStream.next();
String[] values = data.split(",");
tableModel.insertRow(tableModel.getRowCount(), values);
}//end of while block`
重申并完全清楚,我想从 table 中完全删除包含“5/13/2013”的每一行。顺便说一下,我使用的是默认 table 模型。
我最终应用了一个 for 循环和一个 if 语句来让它为我工作。
//remove rows with instances of "5/13/2013"
for (int i = 0; i < tableModel.getRowCount(); i++) {
if (((String)tableModel.getValueAt(i, 0)).equals("5/13/2013")) {
tableModel.removeRow(i);
}//end of if block
}//end of for block
它对我来说效果很好,并且已经摆脱了每一行。希望这可以帮助其他人。
while(i < tableModel.getRowCount()) {
//if the value at (i, 0) match the specified value the row will be removed
/*
if the row removed all row will move up and their index will be changed
so you have to add a condition if the value from the table doesn't match
the specified value the iterator i will iterate by one to jump to the next
row
*/
if (((String)tableModel.getValueAt(i, 0)).equals("5/13/2013")) {
tableModel.removeRow(i);
}else {
++i;
}
}