Java Utililty Methods JTree Expand

List of utility methods to do JTree Expand

Description

The list of methods to do JTree Expand are organized into topic(s).

Method

voidexpandAllNodes(JTree tree, TreeNode node)
expands all nodes in the tree that descend from node
final TreePath treepath = new TreePath(((DefaultMutableTreeNode) node).getPath());
tree.expandPath(treepath);
for (int i = 0; i < node.getChildCount(); i++) {
    expandAllNodes(tree, node.getChildAt(i));
voidexpandByExpansionStateStrings(final JTree tree, final TreePath parent, ArrayList expansionStateStrings)
expand By Expansion State Strings
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);
        expandByExpansionStateStrings(tree, path, expansionStateStrings);
if (isTreePathContainedInExpansionState(parent, expansionStateStrings)) {
    tree.expandPath(parent);
} else {
    tree.collapsePath(parent);
voidexpandFirstNode(JTree tree)
If expand is true, expands all nodes in the tree.
expandFirstNodeAtLevel(getDepth(tree), tree);
voidexpandFirstNodeAtLevel(int level, JTree tree)
expand First Node At Level
if (level == 0)
    return;
TreeNode root = (TreeNode) tree.getModel().getRoot();
TreePath rootPath = new TreePath(root);
TreeNode firstChild = root.getChildAt(0);
TreePath path = rootPath.pathByAddingChild(firstChild);
tree.expandPath(path);
expandNodesAtLevel(level - 1, tree, path);
...
voidexpandFully(JTree tree, TreePath path)
expand Fully
tree.expandPath(path);
TreeNode node = (TreeNode) path.getLastPathComponent();
for (int i = 0; i < node.getChildCount(); i++) {
    expandFully(tree, path.pathByAddingChild(node.getChildAt(i)));
intexpandJTreeNode(javax.swing.JTree tree, javax.swing.tree.TreeModel model, Object node, int row, int depth)
Expands a given node in a JTree.
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;
...
voidexpandLevels(JTree tree, int levels, boolean expand)
expand Levels
TreeModel model = tree.getModel();
if (model == null) {
    return;
TreeNode root = (TreeNode) model.getRoot();
expandLevels(tree, new TreePath(root), levels, expand);
voidexpandNodesAtLevel(int level, JTree tree, TreePath parent)
expand Nodes At Level
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);
...
voidexpandOrCollapseAllRows(final JTree tree, final boolean expand)
Expand or collapse every node in the given tree.
if (tree == null) {
    throw new IllegalArgumentException(new NullPointerException("tree"));
expandOrCollapseAllRows(tree, new TreePath(tree.getModel().getRoot()), expand);
voidexpandOrCollapseJTree(JTree tree, boolean expand)
Expands a JTree completely
Object root = tree.getModel().getRoot();
expandOrCollapseJTree(tree, new TreePath(root), expand);