0

I have a JTable displaying the contents of a log file, and the first column is a timestamp with milliseconds (e.g. 10:31:54.531). The data is already ordered by this column. I would like to allow the user to scroll to the timestamp he is interested in.

Here is a Java snippet of what I would like to do:

String myTimestamp = "10:31:54.531";
Integer myRowIndex = getRowIndexFromStringTimestamp(myTimestamp);
myJTable.getSelectionModel().setSelectionInterval(myRowIndex, myRowIndex);
myJTable.scrollRectToVisible(new Rectangle(myJTable.getCellRect(myRowIndex, 0, true)));

But how to implement getRowIndexFromStringTimestamp?

Thank you

jon
  • 408
  • 6
  • 20

2 Answers2

3
  • I think to use Filtering instead, is easiest than looping and searching in XxxTableModel or JTables view for comparing the desired value

  • have to set proper ColumnClass for (java.util.Date) for column contains Date(DateTime) value

  • otherwise have to loop and search in XxxTableModel or JTables view, with comparing the value

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thank you! I shall try the filtering and get one row index (in model) after filtering, then I can remove the filtering and scroll to this row index I guess – jon Jan 21 '13 at 19:52
3

You need a reference to the table and use a combination JTable#getRowCount, JTable#getValueAt

int matchIndex = -1;
for (int rowIndex = 0; rowIndex < myJTable.getRowCount(); index++) {
    Object value = myJTable.getValueAt(rowIndex, colIndex);
    if (value.equals(timestamp)) { // Or what ever comparison you need..
        matchIndex = rowIndex;
        break;
    } 
}
return matchIndex;

The problem with this is will get slower with more data.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks, I'll give it a try. The users should not load more than 150k rows, which is roughly 1 day of data (it's easier for them to load the entire day and then jump at different point in time) – jon Jan 21 '13 at 20:01
  • 1
    Wouldn't a binary search be appropriate? The entries are sorted after all. – lhballoti Jan 21 '13 at 21:47
  • @lhballoti Assuming the data in the model was sorted, yes. – MadProgrammer Jan 21 '13 at 22:10
  • I just tried it (no binary search) and the performance is fast on 150k rows, so that works for me! – jon Jan 22 '13 at 10:53