Domanda

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).

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top