我正在创建一个这样的 JTable:
String[] colName = new String[] {
"ID#", "Country", "Name", "Page titel", "Page URL", "Time"
};
Object[][] products = new Object[][] {
{
"123", "USA", "Bill", "Start", "http://www.url.com", "00:04:23"
},
{
"55", "USA", "Bill", "Start", "http://www.url.com", "00:04:23"
}
};
dtm = new DefaultTableModel(products, colName);
table = new JTable(dtm);
如何按 ID 更新行?我想更新 ID 等于 55 的整行。
编辑:我知道如何通过行 ID 进行删除,但我如何实际更新单元格?
public void removeVisitorFromTable(String visitorID) {
int row = -1; //index of row or -1 if not found
//search for the row based on the ID in the first column
for(int i=0;i<dtm.getRowCount();++i)
if(dtm.getValueAt(i, 0).equals(visitorID)) {
row = i;
break;
}
if(row != -1) {
dtm.removeRow(row);//remove row
} else {
}
}