2

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.

palcan
  • 73
  • 1
  • 6
  • please try to get your technical vocabulary half-way correct: focus (aka: lead selection index in table context) has nothing to do with visibility of a row. The scrolling should work in both directions. So there's something wrong with the code you are not showing, time for an sscce, as @camickr already suggested – kleopatra Nov 14 '11 at 16:21
  • Same here. Initially I was using `table.changeSelection`, then I changed to `table.scrollRectToVisible`. In both cases I get the described behavior: scrolls down but not up. – Gustavo Jul 03 '17 at 21:07

2 Answers2

3

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

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(...);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • does it work? can you pls give a sample code how to set a focus on row with number=i using your approach? – palcan Nov 14 '11 at 09:40
  • You should be able to replace your scrollRectToVisible() code with the one line of code I suggested above. If you have problems then post your [SSCCE](http://www.sscce.org) that demonstrates the problem. – camickr Nov 14 '11 at 16:00
  • wouldn't recommend that - it's not a one-liner anyway, as you need calculate the viewPosition ;-) – kleopatra Nov 14 '11 at 16:24
  • @kleopatra, Not sure what you don't recommend. The view position is just the (x, y) of the Rectangle returned from the getCellRect(..). Ok, 2 lines of code, one the get the Rectangle, the other to create the Point from the Rectangle. – camickr Nov 14 '11 at 16:49
  • assuming that you want the _cell_ scroll to the upper right corner. Which typically is not exactly the requirement (not sure what it is here, the OP wasn't overly verbose - my guess is that something is simply wrong with her/his code). The thingy I'm always wary about is calling a method on a referenced object - can be done, obviously, and even might look easiest on first sight. More often than not it's a sign of something missing (in the api of the scrollPane, f.i.) or not fully defined or understood (by me :-) Getting user pleasing balanced scroll correct requires surprisingly many thoughts – kleopatra Nov 14 '11 at 22:35
  • @kleopatra, yes, scrollRectToVisible(..) should work in both directions. For some reason I was thinking the poster wanted the row to be displayed at the top of the viewport. So forget my suggestion. – camickr Nov 15 '11 at 06:28