Here you can find the source of autoScrollIsDesirable(JTree tree)
Determines whether a tree should be in auto scroll mode.
Parameter | Description |
---|---|
tree | The target of this query. |
public static boolean autoScrollIsDesirable(JTree tree)
//package com.java2s; //License from project: Apache License import java.awt.*; import javax.swing.*; public class Main { /**/* w w w. j a v a 2s. co m*/ * <p> * Determines whether a tree <i>should</i> be in auto scroll mode. The logic * is as follows: Auto scroll is <i>desirable</i> if:</p> * <ul> * <li>No selection is visible</li> * AND * <li>The bottom of the tree is visible</li> * </ul> * <p> * MUST be invoked from EDT.</p> * * @param tree The target of this query. * @return True if the tree <i>should</i> be in auto scroll mode, false * otherwise. */ public static boolean autoScrollIsDesirable(JTree tree) { boolean ret = false; if (tree.getRowCount() < 1) { // Empty tree return ret; } Rectangle viewRect = tree.getVisibleRect(); int[] selected = tree.getSelectionRows(); boolean selectedIsVisible = false; if (selected == null) { selected = new int[0]; } for (int row : selected) { if (viewRect.intersects(tree.getRowBounds(row))) { selectedIsVisible = true; break; } } if (!selectedIsVisible) { Rectangle bottomBound = tree.getRowBounds(tree.getRowCount() - 1); ret = viewRect.intersects(bottomBound); } return ret; } }