Example usage for javax.swing JTree expandPath

List of usage examples for javax.swing JTree expandPath

Introduction

In this page you can find the example usage for javax.swing JTree expandPath.

Prototype

public void expandPath(TreePath path) 

Source Link

Document

Ensures that the node identified by the specified path is expanded and viewable.

Usage

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel(new BorderLayout());
    final JTree tree = new JTree();
    panel.add(new JScrollPane(tree));
    JButton btn = new JButton("Press Me");
    btn.addActionListener(et -> {/* w  w  w. ja v a  2  s  .c  o m*/
        for (Enumeration e = ((TreeNode) tree.getModel().getRoot()).children(); e.hasMoreElements();) {
            TreeNode tn = (TreeNode) e.nextElement();
            tree.expandPath(new TreePath(((DefaultTreeModel) tree.getModel()).getPathToRoot(tn)));
        }
    });
    panel.add(btn, BorderLayout.SOUTH);
    JFrame frame = new JFrame("");
    frame.getContentPane().add(panel);
    frame.setSize(300, 300);
    frame.setLocation(100, 100);
    frame.pack();
    frame.show();
}

From source file:Main.java

public static void expandTreeNode(JTree tree, DefaultMutableTreeNode node, int depth) {
    if (depth <= 0) {
        return;//www. ja v  a2 s.c om
    }
    tree.expandPath(new TreePath(node.getPath()));
    for (int i = 0; i < node.getChildCount(); i++) {
        expandTreeNode(tree, (DefaultMutableTreeNode) node.getChildAt(i), depth - 1);
    }
}

From source file:Main.java

/** Fully expands the given JTree from the specified node. */
public static void expandTree(final JTree tree, final DefaultMutableTreeNode node) {
    if (node.isLeaf())
        return;/*from  w ww .  ja v a2s  .c  o  m*/
    tree.expandPath(new TreePath(node.getPath()));
    final Enumeration e = node.children();
    while (e.hasMoreElements()) {
        expandTree(tree, (DefaultMutableTreeNode) e.nextElement());
    }
}

From source file:Main.java

public static void loadExpansionState(JTree tree, Enumeration<TreePath> enumeration) {
    if (enumeration != null) {
        while (enumeration.hasMoreElements()) {
            TreePath treePath = enumeration.nextElement();
            tree.expandPath(treePath);
        }//  w ww. j  a  va2s  .  c om
    }
}

From source file:Main.java

public static void expand(JTree tree, int level) {
    DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) tree.getModel().getRoot();
    while (currentNode != null) {
        if (currentNode.getLevel() <= level) {
            tree.expandPath(new TreePath(currentNode.getPath()));
        }//  www  .  ja v  a2  s  .  c om
        currentNode = currentNode.getNextNode();
    }
}

From source file:Main.java

private static void expandAll(JTree tree, TreePath parent, boolean expand) {
    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);
            expandAll(tree, path, expand);
        }//w ww .jav a2  s .c o m
    }

    if (expand)
        tree.expandPath(parent);
    else
        tree.collapsePath(parent);
}

From source file:Main.java

/** 
 * Expand (or collapse) the children nodes of specified node of a JTree for a certain depth.
 * @param tree Tree of which we want to expand the nodes.
 * @param parent Parent node.//from ww w  .  j  ava  2 s.  c  o  m
 * @param expand <code>true</code> to expand the nodes.  <code>false</code> to collapse the nodes. 
 * @param depth Depth of children to expand or collapse.
 */
public static void expandNodes(JTree tree, TreePath parent, boolean expand, int depth) {
    if (parent == null || depth == 0)
        return;

    Object node = parent.getLastPathComponent();
    if (node != null) {
        for (int i = 0; i < tree.getModel().getChildCount(node); i++) {
            Object child = tree.getModel().getChild(node, i);
            TreePath path = parent.pathByAddingChild(child);
            expandNodes(tree, path, expand, depth - 1);
        }
    }

    if (expand)
        tree.expandPath(parent);
    else
        tree.collapsePath(parent);
}

From source file:TreeUtil.java

public static void expandAll(JTree tree, TreePath parent, boolean expand) {
    // Traverse children
    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);
            expandAll(tree, path, expand);
        }//from w w  w .ja va  2  s  .  co m
    }
    // Expansion or collapse must be done bottom-up
    if (expand) {
        tree.expandPath(parent);
    } else {
        tree.collapsePath(parent);
    }
}

From source file:Main.java

static private void expandAll(JTree tree, TreePath parent, boolean expand) {
    // Traverse children
    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);
            expandAll(tree, path, expand);
        }//ww  w  .  j  a v a2s.  c om
    }
    // Expansion or collapse must be done bottom-up
    if (expand) {
        tree.expandPath(parent);
    } else {
        tree.collapsePath(parent);
    }
}

From source file:TreeUtils.java

public static void expandAll(JTree tree, TreePath parent, boolean expand) {
    // Traverse children
    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);
            expandAll(tree, path, expand);
        }/*from  www  .j a v a 2s .c o  m*/
    }

    // Expansion or collapse must be done bottom-up
    if (expand) {
        tree.expandPath(parent);
    } else {
        tree.collapsePath(parent);
    }
}