【问题标题】:unable to do consecutive search无法进行连续搜索
【发布时间】:2013-01-20 23:49:34
【问题描述】:
 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 屏幕中,如果我进行第二次搜索,即使匹配了关键字,也找不到任何结果。我很确定最后搜索的索引被保留而不是重置是问题,但我不确定在哪里以及如何更改它。

【问题讨论】:

    标签: java swing for-loop jtable abstracttablemodel


    【解决方案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 时,您将找到第二个匹配项。
    • 等等……

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

    int j = fromCol
    

    应该替换为

    int j = (i == fromRow ? fromCol : 0);
    

    【讨论】:

    • 已尝试您的更改,搜索不灵活。匹配的时间仍然没有显示。当我到达最后一行时,您无法搜索前一行。即在第 7 行找到某人,然后在第二次搜索第 5 行匹配的项目即使匹配也不起作用,第一次搜索后第一列也完全无法搜索
    • @MooHa 这是纯算法逻辑。如果您到达表的末尾并想要执行某种“换行搜索”(即,还要从表的开头检查项目),那么您需要在之后重置您的计数器。当然,您必须确保如果您找到当前选定的单元格,则没有找到其他匹配项。
    【解决方案2】:

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

    你为什么不首先将fromRowfromCol 设置为0、0?

    【讨论】:

    • 我刚试过这个,仍然不正确,有时即使匹配也不会拾取第一列中的值,但对于同一行,会找到第二列或第三列:/跨度>
    • 如果他将 fromRow 和 fromCol 重置为 0,0,那么他将不断找到相同的单元格。他正在尝试执行增量搜索。
    猜你喜欢
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多