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

我上面的搜索方法有问题。我这样做的方式,它只允许我搜索一个特定的匹配关键字一次。在同一个 GUIscreen 中,如果我进行第二次搜索,即使匹配了关键字,也找不到任何结果。我很确定最后搜索的索引被保留并且没有重置是问题,但我不确定在哪里以及如何更改它。

4

2 回答 2

1

您正在将fromRowand fromColvars 设置为选定的行和列。然后您将选择更改为找到第一个结果的位置。如果第二次搜索只在当前选择的左侧或上方找到了东西,它将找不到任何东西。

你为什么不一开始就设置fromRowandfromCol为 0, 0 呢?

于 2013-02-05T22:22:47.840 回答
1

假设您有一个 10 行 5 列的表格。

有比赛:

  • 2, 2
  • 4, 1
  • 9, 0

  • 第一次你会发现 2, 2。

  • 因此,下次您从第 2 行和第 3 列开始。您的算法将仅在第 3 列和第 4 列中查找值(4 是表的最后一列)。

你应该拥有的是:

  • 首先从单元格 2、3 到单元格 2、4
  • 然后使用你的循环从第 3 行和第 0 列开始并增加列 - >第 3 行不匹配
  • 然后将行增加到 4 并将列重置为 0。当您将列增加到 1 时,您将找到第二个匹配项。
  • ETC...

我还没有测试过,但我认为在你的内部循环中,你应该像这样启动增量

int j = fromCol

应该替换为

int j = (i == fromRow ? fromCol : 0);
于 2013-02-05T23:50:15.003 回答