简体   繁体   中英

JTable doesn't scroll up

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM