Reputation: 3988
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
Upvotes: 1
Views: 102
Reputation: 51485
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.
Upvotes: 3
Reputation: 109813
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
Upvotes: 3