Java JTree Expand expandFirstNode(JTree tree)

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

Description

If expand is true, expands all nodes in the tree.

License

BSD License

Parameter

Parameter Description
tree a parameter
expand a parameter

Declaration

static void expandFirstNode(JTree tree) 

Method Source Code

//package com.java2s;
/*   TouchStone design platform is a software to design protocols for lab        *
 *   experiments. It is published under the terms of a BSD license               *
 *   (see details below)                                                         *
 *   Author: Caroline Appert (appert@lri.fr)                                     *
 *   Copyright (c) 2010 Caroline Appert and INRIA, France.                       *
 *   TouchStone design platform reuses parts of an early version which were      *
 *   programmed by Matthis Gilbert.                                              *
 *********************************************************************************/

import java.util.Enumeration;

import javax.swing.JTree;

import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public class Main {
    /**/*  w w w.ja v a  2 s .  c  om*/
     *   If expand is true, expands all nodes in the tree.
     * Otherwise, collapses all nodes in the tree.
     * @param tree
     * @param expand
     */
    static void expandFirstNode(JTree tree) {
        expandFirstNodeAtLevel(getDepth(tree), tree);
    }

    static void expandFirstNodeAtLevel(int level, JTree tree) {
        if (level == 0)
            return;
        TreeNode root = (TreeNode) tree.getModel().getRoot();
        // Traverse tree from root
        TreePath rootPath = new TreePath(root);
        TreeNode firstChild = root.getChildAt(0);
        TreePath path = rootPath.pathByAddingChild(firstChild);
        tree.expandPath(path);
        expandNodesAtLevel(level - 1, tree, path);
    }

    static int getDepth(JTree tree) {
        TreeNode root = (TreeNode) tree.getModel().getRoot();
        if (root.getChildCount() == 0)
            return 0;
        else
            return 1 + getDepth(root.getChildAt(0));
    }

    static int getDepth(TreeNode tree) {
        if (tree.getChildCount() == 0)
            return 0;
        else
            return 1 + getDepth(tree.getChildAt(0));
    }

    static void expandNodesAtLevel(int level, JTree tree, TreePath parent) {
        if (level == 0)
            return;
        TreeNode node = (TreeNode) parent.getLastPathComponent();
        if (node.getChildCount() >= 0) {
            for (Enumeration<?> e = node.children(); e.hasMoreElements();) {
                TreeNode n = (TreeNode) e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                tree.expandPath(path);
                expandNodesAtLevel(level - 1, tree, path);
            }
        }
    }
}

Related

  1. expandAllNodes(JTree tree)
  2. expandAllNodes(JTree tree, int startingIndex, int rowCount)
  3. expandAllNodes(JTree tree, int startingIndex, int rowCount)
  4. expandAllNodes(JTree tree, TreeNode node)
  5. expandByExpansionStateStrings(final JTree tree, final TreePath parent, ArrayList expansionStateStrings)
  6. expandFirstNodeAtLevel(int level, JTree tree)
  7. expandFully(JTree tree, TreePath path)
  8. expandJTreeNode(javax.swing.JTree tree, javax.swing.tree.TreeModel model, Object node, int row, int depth)
  9. expandLevels(JTree tree, int levels, boolean expand)