Scroll a JTree so that last line is at top of JScrollPane in Java
I have a large JTree (many nodes) displaying in a JScrollPane. I would like to allow the last tree node to display at the top of the JScrollPane. Swing does not seems to allow this by default, showing the (fully expanded) JTree with the last node at the bottom of the JScrollPane. The JScrollB开发者_C百科ar thumb is touching the bottom of its track.
I am using the JViewport.setViewPosition() method to set the upper-left location of the tree node to the upper-left position in the JViewport, but it refuses to do so if that would cause it to display "whitespace" at the bottom of the JTree.
I have tried manipulating the JViewport by setting the size (.setViewSize to its old value + an extra height of .getExtentSize()), but it still refuses to allow the thumb to move down, even though it is now not at the bottom of its track. I am adjusting the JViewport size in an event handler (.addChangeListener).
I suppose you could override the preferred size of the JTree to be the default preferred size plus the extent size of the viewport minus the height of one row of the JTree.
What if you put your JTree inside a VBox with empty space at the bottom, and then wrap the VBox with the JScrollPane?
精彩评论