I have a JTable with a JScrollPane. There is something i do not understand with the scrollPane viewport … I am now selecting row number 1000 in the table, so many rows above it are not visible on the screen. Now when i check if row 0 is visible in the current viewport, it says ‘Yes’.
Here is my code :
JViewport viewport = scrollPane1.getViewport();
Rectangle rect = table1.getCellRect( 0, 1, true );
// Check if view completely contains the row 0 :
if( viewport.contains( rect.getLocation() ) )
System.out.println( "The current view contains row 0" );
This code always returns true, and the text is printed, whatever the row i am standing on. Am i missing something here ?
The method you’r looking for is
it’s defined in JComponent, in your context you use it
edit: just realized that you probably are still scratching your head – even though all answers already given are technically correct 🙂
Basically, it’s all about coordinate systems, that is a location relative to a given origin. When using location related methods, you have to be aware of the coordinate system for that particular method is and you can’t mix different systems (at least not without translating the one or other).
But mixing you did:
The solution is to find a coordinate system where the cell location as found above makes sense (or translate the cell location into the parent system manually, but that’s not needed here), there are methods which do the translation:
querying the table itself (as opposed to querying its parent) is preferable, because it doesn’t need any knowlegde about its parent.