Implementing the Scrollable interface requires implementing the getPreferredScrollableViewportSize() method. This is typically done by just forwarding the call to getPreferredSize() - except when other parameters of the Scrollable may affect the preferred JViewport size, such as the setVisibleRowCount() method in JTree.
I have a situation where I think this method can help me achieve my goals, but a simple print statement in my implementation of getPreferredScrollableViewportSize() confirms that it is never called. A search of JScrollPane, ScrollPaneLayout and JViewport confirms there are no (direct) calls to that method. Yet, comments in JScrollPane specifically state that ScrollPaneLayout uses it, and I can confirm it is implemented as expected in JTree.
When is it called, by what class (presumably a LayoutManager) and when? I'm using JDK 1.7_07
I've not had the time to search through all of the source code, but on testing, it looks like the method is called when the GUI is packed.
which returns:
Edit 2
When I changed my getPreferredScrollableViewportSize override to:
This is what I saw:
suggesting that the ViewportLayout class's preferredLayoutSize is what calls the Scrollable's getPreferredScrollableViewportSize method.
Edit 3
And in fact, the ViewportLayout source code supports this: