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 屏幕中,如果我进行第二次搜索,即使匹配关键字,也找不到结果。我很确定最后搜索的索引被保留并且没有重置是问题所在,但我不确定在哪里以及如何更改它。
最佳答案
您正在将 fromRow
和 fromCol
变量设置为选定的行和列。然后您将选择更改为找到第一个结果的位置。如果第二次搜索仅找到当前选择左侧或上方的内容,则不会找到任何内容。
为什么不首先将 fromRow
和 fromCol
设置为 0, 0?
关于java - 无法连续搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14717915/