Java JTree autoScrollIsDesirable(JTree tree)

Here you can find the source of autoScrollIsDesirable(JTree tree)

Description

Determines whether a tree should be in auto scroll mode.

License

Apache License

Parameter

Parameter Description
tree The target of this query.

Return

True if the tree should be in auto scroll mode, false otherwise.

Declaration

public static boolean autoScrollIsDesirable(JTree tree) 

Method Source Code


//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;
    }
}

Related

  1. applyDefaultProperties(final JTree comp)
  2. autoscroll(JTree tree, Point cursorLocation)
  3. clearTreeIcon(JTree tree)
  4. correctRowHeight(JTree tree)
  5. disposeExpressionsTree()
  6. enableAutoExpansion(final JTree tree)