Search code examples
javaswingjtablejscrollpaneviewport

keep row in view on same position


I have a table where i can add some filters to the data being displayed in the table, i would like to be able to keep a specific row on the same position on the screen, for selecting row i have chosen the top row since its the only row i know is there if there is any data(could be the only row), when the filters change i need to scroll to that row (or one near it), i can find the row number of the item i want to go to after changing data i want to go to.

Currently im using jTable.scrollRectToVisible(new Rectangle(jTable.getCellRect(rowId, 0, true))); but that just makes sure its in view. and since in most cases the change of data have made my view go to top it scrolls the row to the bottom of the view. How do i best get the row to the top of my view? in case the row is at the end of my data it should just scroll to the bottom, not force blank space in the scroll box to get it to the top.


Solution

  • scrollPane.getViewport().setViewPosition(Point);
    

    The Point would be the x/y position return from the getCellRect(...) method.