1

I have a JTable and, inside its TableModel, I saved an int; this is the row index that I want my view to "keep in the middle" of the Viewport.
What I mean is that, for example, if I have 1000 rows, 10 are shown in the View at any time and my index is 500, I want my vieport to automatically show rows 495-504. These rows are not necessarily selected. I hope I was able to explain my problem properly.

Thank you

mdm
  • 3,838
  • 3
  • 24
  • 40

2 Answers2

3

1) JTable (notice can by caused by Filtering) returns number of rows

2) each of row can returns Rectangle

3) put this Rectangle to the JScrollPane#scrollRectToVisible(Rectangle aRect) wrapped into invokeLater(), simple example about moving JViewport to the decision row here

Community
  • 1
  • 1
mKorbel
  • 109,107
  • 18
  • 130
  • 305
3

Assuming your row heights are the same,

int rowPosition = index - 10 / 2;
int pixelPosition = rowPosition * table.getRowHeight(rowPosition);
scrollPane.getViewPort().setViewPosition(new Point(0, pixelPosition));

If the row heights aren't the same, you would have to sum all of the row heights from row 1 to row index - 5.

Gilbert Le Blanc
  • 48,182
  • 6
  • 65
  • 110