ric*_*rge 2 java indexing swing row jtable
在Java中,JTable是否可以通过查找该行中包含的指定值来获取行的索引?我正在使用自定义表模型.例如,考虑一个包含三列和三行的表:
Row 1 = A, B, C
Row 2 = D, E, F
Row 3 = G, H, I
Run Code Online (Sandbox Code Playgroud)
如果我所知道的是表中某处有"F"值,我怎样才能找到值为"F"的行的索引?
如果表模型可用,则蛮力方法是按行和列循环,并将给定值(例如,"F")与结果进行比较getValueAt(row, column)
.示例代码:
int getRowByValue(TableModel model, Object value) {
for (int i = model.getRowCount() - 1; i >= 0; --i) {
for (int j = model.getColumnCount() - 1; j >= 0; --j) {
if (model.getValueAt(i, j).equals(value)) {
// what if value is not unique?
return i;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14244 次 |
最近记录: |