Java examples for Swing:JTree
JTree scroll To Selection
//package com.java2s; import java.awt.Point; import java.awt.Rectangle; import javax.swing.JTree; import javax.swing.JViewport; public class Main { public static void scrollToSelection(JTree tree) { if (!(tree.getParent() instanceof JViewport)) return; setViewPortPosition((JViewport) tree.getParent(), tree.getPathBounds(tree.getSelectionPath())); }//from w w w. ja v a 2 s . c o m private static void setViewPortPosition(JViewport viewport, Rectangle position) { // The location of the viewport relative to the object Point pt = viewport.getViewPosition(); // Translate the cell location so that it is relative // to the view, assuming the northwest corner of the // view is (0,0) position.setLocation(position.x - pt.x, position.y - pt.y); // Scroll the area into view viewport.scrollRectToVisible(position); } }