Skip to content

Instantly share code, notes, and snippets.

@AravindaM
Last active June 27, 2016 02:42
Show Gist options
  • Save AravindaM/66be1f0a87764d639aa8 to your computer and use it in GitHub Desktop.
Save AravindaM/66be1f0a87764d639aa8 to your computer and use it in GitHub Desktop.
[JAVA][JTABLE] Go to given row,column in a JTable and select
Public Class JTableRowSelector{
public static void selectRow(JTable table, int rowIndex, int vColIndex) {
if (!(table.getParent() instanceof JViewport)) {
return;
}
JViewport viewport = (JViewport)table.getParent();
// This rectangle is relative to the table where the
// northwest corner of cell (0,0) is always (0,0).
Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);
// The location of the viewport relative to the table
Point pt = viewport.getViewPosition();
// Translate the cell location so that it is relative
// to the view, assuming the northwest corner of the
// view is (0,0)
rect.setLocation(rect.x-pt.x, rect.y-pt.y);
table.scrollRectToVisible(rect);
table.setRowSelectionInterval(rowIndex,rowIndex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment