Returns whether a JTree node is above another node (one of it's parents). - Java Swing

Java examples for Swing:JTree

Description

Returns whether a JTree node is above another node (one of it's parents).

Demo Code


//package com.java2s;

import javax.swing.tree.TreeNode;

public class Main {
    /**//from w w  w.  j a va  2 s  .c o  m
     * Returns whether a node is above another node (one of it's parents).
     * <p>
     * Moves up from <code>below</code> to all parents until <code>above</code>
     * is found or a parent node above has no parent.
     * <p>
     * Compares the nodes object <strong>references</strong>
     *
     * @param  above node that shall be above <code>below</code>
     * @param  below node that shall be below <code>above</code>
     * @return       true if <code>above</code> is one of <code>below</code>'s
     *               parents
     */
    public static boolean isAbove(TreeNode above, TreeNode below) {
        if (above == null) {
            throw new NullPointerException("above == null");
        }

        if (below == null) {
            throw new NullPointerException("below == null");
        }
        TreeNode parent = below.getParent();
        while (parent != null) {
            if (parent == above) {
                return true;
            }
            parent = parent.getParent();
        }
        return false;
    }
}

Related Tutorials