javaswingjtablejscrollbar

Is there an function like "changeSelection()" to select row but on top of the table not on bottom?


I have a jTable with the tree which automatically selects the corresponding row from previous action. But changeSelection scroll and select row as last visible on bottom. For me is uncomfortable because is tree and is much better to be scrolled and selected row as first visible on top of the table.

I am try table.scrollRectToVisible(table.getCellRect(row,column, true)); same result and tried custom set scrollBar value but I don't know right value because panel can change size.

I expect that automatic scroll to row and that row will be marked on top of the table. Now automatic scroll to row and is marked on bottom of the table.


Solution

  • The scrollRectToVisible(...) method only scrolls the JViewport of the JScrollPane to make sure the Rectangle is visible. So when you scroll down. It will display at the bottom. If you are scrolling up it will display at the top.

    An easier way to control the scrolling is to set the JViewport position yourself:

    JViewport viewport = (JViewport)scrollPane.getViewport();
    Rectangle rectangle = table.getCellRect(row, column, true);
    Point point = new Point(rectangle.x, rectangle.y);
    viewport.setViewPosition( point );