如何从Jtable中删除具有特定值的行?

时间:2017-04-29 00:04:18

标签: java swing jtable

我有一个Jtable,我已经加载了来自.csv文件的值,但它为文件中显示的每个5/13/2013实例创建了一个新行,如下所示:

https://i.stack.imgur.com/phdTs.jpg

我想从表中删除包含此信息的所有行,但我不知道该怎么做。对我有什么建议吗?

以下是我将代码添加到表中的代码,如果有帮助的话:

    while (inputStream.hasNext()) {
        String data = inputStream.next();
        String[] values = data.split(",");
        tableModel.insertRow(tableModel.getRowCount(), values);
    }//end of while block`

重申并完全清楚,我想完全从表中删除包含“5/13/2013”​​的每一行。顺便说一句,我正在使用deafault table模型。

2 个答案:

答案 0 :(得分:0)

我最终应用了一个for循环和一个if语句来让这个为我工作。

   grid.Column("Del", "Delete", format: @<text><span>
                                                                    @using (Html.BeginForm("Index", "OutReach", FormMethod.Get, new { @enctype = "multipart/form-data", @id = "addethinicty" }))
                                                                    {
                                                                        <button type="submit" class="btn btn-default">Submit</button>
                                                                    }


                    </span></text>)

它对我来说效果很好,并且已经摆脱了每一行。希望这可以帮助其他人。

答案 1 :(得分:0)

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;
}

}