Java JTree Expand expandJTreeNode(javax.swing.JTree tree, javax.swing.tree.TreeModel model, Object node, int row, int depth)

Here you can find the source of expandJTreeNode(javax.swing.JTree tree, javax.swing.tree.TreeModel model, Object node, int row, int depth)

Description

Expands a given node in a JTree.

License

Open Source License

Parameter

Parameter Description
tree The JTree to expand.
model The TreeModel for tree.
node The node within tree to expand.
row The displayed row in tree that represents node.
depth The depth to which the tree should be expanded. Zero will just expand node, a negative value will fully expand the tree, and a positive value will recursively expand the tree to that depth relative to node.

Declaration

public static int expandJTreeNode(javax.swing.JTree tree, javax.swing.tree.TreeModel model, Object node,
        int row, int depth) 

Method Source Code

//package com.java2s;
/*//from  w w  w .  j  av a2s.  co m
Copyright 2007-2009 QSpin - www.qspin.be
    
This file is part of QTaste framework.
    
QTaste is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
QTaste is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public License
along with QTaste. If not, see <http://www.gnu.org/licenses/>.
*/

public class Main {
    /**
     * Expands a given node in a JTree.
     *
     * @param tree      The JTree to expand.
     * @param model     The TreeModel for tree.     
     * @param node      The node within tree to expand.     
     * @param row       The displayed row in tree that represents
     *                  node.     
     * @param depth     The depth to which the tree should be expanded. 
     *                  Zero will just expand node, a negative
     *                  value will fully expand the tree, and a positive
     *                  value will recursively expand the tree to that
     *                  depth relative to node.
     */
    public static int expandJTreeNode(javax.swing.JTree tree, javax.swing.tree.TreeModel model, Object node,
            int row, int depth) {
        if (node != null && !model.isLeaf(node)) {
            tree.expandRow(row);
            if (depth != 0) {
                for (int index = 0; row + 1 < tree.getRowCount() && index < model.getChildCount(node); index++) {
                    row++;
                    Object child = model.getChild(node, index);
                    if (child == null)
                        break;
                    javax.swing.tree.TreePath path;
                    while ((path = tree.getPathForRow(row)) != null && path.getLastPathComponent() != child)
                        row++;
                    if (path == null)
                        break;
                    row = expandJTreeNode(tree, model, child, row, depth - 1);
                }
            }
        }
        return row;
    }
}

Related

  1. expandAllNodes(JTree tree, TreeNode node)
  2. expandByExpansionStateStrings(final JTree tree, final TreePath parent, ArrayList expansionStateStrings)
  3. expandFirstNode(JTree tree)
  4. expandFirstNodeAtLevel(int level, JTree tree)
  5. expandFully(JTree tree, TreePath path)
  6. expandLevels(JTree tree, int levels, boolean expand)
  7. expandNodesAtLevel(int level, JTree tree, TreePath parent)
  8. expandOrCollapseAllRows(final JTree tree, final boolean expand)
  9. expandOrCollapseJTree(JTree tree, boolean expand)