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

I am having a problem with my search method above. The way I did it, It only allows me to search a particular matched keyword once. While being in the same GUIscreen, if I do a second search, even if a keyword is matched, no result is found. I am pretty sure the last searched index is kept and not reset is the problem, but Im unsure where and how to change this.

MooHa
  • 819
  • 3
  • 11
  • 23

2 Answers2

1

You are setting the fromRow and fromCol vars to be the selected row and column. And then you are changing the selection to be where the first result is found. If the second search would have only found things to the left or above the current selection, it won't find anything.

Why don't you just set fromRow and fromCol to be 0, 0 in the first place?

I82Much
  • 26,901
  • 13
  • 88
  • 119
  • i've just tried this, still not correct, at times the value in first column is not picked up even if matched, but for that same row, 2nd or 3rd columns gets found :/ – MooHa Feb 05 '13 at 22:32
  • 1
    If he resets the fromRow and fromCol to 0,0, then he will find constantly the same cell. He is trying to perform an incremental search. – Guillaume Polet Feb 05 '13 at 23:51
1

Let's say you have a table of 10 row with 5 columns.

There are matches in:

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

  • First time you will find 2, 2.

  • So next time you start in row 2 and column 3. Your algorithm will only look for values in column 3 and 4 (4 is the last column of your table).

What you should have is:

  • first look from cell 2, 3 until cell 2, 4
  • Then use your loops to start from row 3 and column 0 and increment columns--> no match on row 3
  • Then increment row to 4 and reset column to 0. As you increment column to 1, you will then find your second match.
  • etc...

I have not tested yet, but I think that in your inner-loop, you should initiate the increment like this

int j = fromCol

should be replaced by

int j = (i == fromRow ? fromCol : 0);
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • having tried your changes, search is not flexible. matched time is still not displayed. When I reach last row, you cant search previous rows. i.e found someone on row 7, then on second search row 5 matched item wouldnt work even if matched, also first column is completely unsearchable after first search – MooHa Feb 06 '13 at 01:05
  • @MooHa This is pure algorithmic logic. If you reach the end of the table and want to perform some kind of "wrap search" (ie, also check items from the beginning of the table), then you need to reset your counters afterwards. Of course, you have to make sure that if you find the currently selected cell, then no other match has been found. – Guillaume Polet Feb 06 '13 at 08:51