JTable doesn't scroll up

693 views Asked by At

I have JScrollPane with a JTreeTable. Typically my JTable contains a lot of items and app must set focus to specific row in treetable and scroll to it. I'm using the following recommended code to set focus:

table.scrollRectToVisible(table.getCellRect(rowIndex, vColIndex, true));

For some reason that works only if the newly selected row is below the current visible rows. But when the program asks to select an invisible row that is Above the current visible row it doesn't scroll to that position at all. I've googled a lot about the issue but haven't found any solution yet. Has someone an idea how to fix that problem? Thank you in advance.

2

There are 2 answers

6
camickr On

The scrollRectToVisible() method only makes sure the rectangle is visible in the viewport. I believe you can make the Rectangle the same size as the viewport to force a scroll even when the starting point is visible.

An easier approach is to use:

scrollPane.getViewport().setViewPosition(...);
0
Ari Stassart On

To handle the scroll up condition the following work-around worked for me:

table.scrollRectToVisible(table.getCellRect(0, 0, true));
table.scrollRectToVisible(table.getCellRect(rowIndex, vColIndex, true));