public boolean searchSummaryData(String textToFind) {
int fromRow, fromCol;
fromRow = summaryTable.getSelectedRow();
fromCol = summaryTable.getSelectedColumn();
if (fromRow < 0) {
fromRow = 0; //set to start point, first row
}
if (fromCol < 0) {
fromCol = 0;
} else {
fromCol++;//incremental search - look through each columns, then switch to next row
if (fromCol >= summaryTable.getColumnCount()) {
fromCol = 0;
fromRow++;
}
}
for (int i = fromRow; i < summaryTableModel.getRowCount(); i++) {
for (int j = fromCol; j < summaryTableModel.getColumnCount(); j++) {
final Object valueAt = summaryTableModel.getValueAt(i, j); //point to object at i,j
if (valueAt != null) {
textToFind = textToFind.toLowerCase();
if (valueAt.toString().toLowerCase().contains(textToFind)) {
//Map the index of the column/row in the table model at j/i to the index of the column/row in the view.
int convertRowIndexToView = summaryTable.convertRowIndexToView(i);
int convertColIndexToView = summaryTable.convertColumnIndexToView(j);
summaryTable.setRowSelectionInterval(i, i);
summaryTable.setColumnSelectionInterval(j, j);
//Return a rectangle for the cell that lies at the intersection of row and column.
Rectangle rectToScrollTo = summaryTable.getCellRect(convertRowIndexToView, convertColIndexToView, true);
tableSp.getViewport().scrollRectToVisible(rectToScrollTo);
return true;
}
}
}
}
return false;
}
我遇到了上述搜索方法的问题。我这样做,它只允许我搜索一次特定的匹配关键字。在同一个GUI屏幕中,如果我进行第二次搜索,即使匹配关键字,也不会找到任何结果。我很确定上次搜索到的索引是保留的,而不是重置是问题,但我不确定在哪里以及如何更改。无法执行连续搜索
我刚刚试过这个,仍然不正确,有时即使匹配,第一列中的值也没有被拾取,但是对于同一行,找到第二或第三列:/ – MooHa
如果他重置fromRow和fromCol到0,0,那么他会不断找到相同的单元格。他正在尝试执行增量搜索。 –
啊,很好的电话。我不明白该方法的名称。 – I82Much