I have a Swing JTable. It is large and users can scroll up and down to see the row of data they want to work on. When the user stops scrolling, I want to know the row number of the JTable that is the first visible row that the user can see. I want to use this to scroll back to this position in the table after the user is through additional operations (I know how to do the scrolling piece - its getting the first visible row that has me stumped).

有帮助吗?

解决方案

You first need to find out which part of the table is visible and then map the visual coordinates to the underlying row:

JViewport viewport = scrollPane.getViewport();
Point p = viewport.getViewPosition();
int rowIndex = table.rowAtPoint(p);

In addition to that you might want to experiment with offsets to p (such as, e.g., offsetting it by half a row height etc.) depending on the behavior you want to achieve when the first visible row is only partly visible.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top