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

voidexpandPath(JTree tree, TreePath tp)
expand Path
Object root = tree.getModel().getRoot();
expandPath(tree, new TreePath(root), tp, 0);
voidexpandPathOnEdt(final JTree tree, final TreePath path)
expand Path On Edt
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        tree.expandPath(path);
});
voidexpandPaths(JTree tree, Collection paths)
expand Paths
if (paths != null) {
    for (Iterator<TreePath> it = paths.iterator(); it.hasNext();) {
        TreePath tp = it.next();
        expandPath(tree, tp);
voidexpandTree(JTree tree, boolean expand)
expand Tree
TreeNode root = (TreeNode) tree.getModel().getRoot();
expandAll(tree, new TreePath(root), expand);
voidexpandTree(JTree tree, TreeNode start, int level)
expand Tree
for (Enumeration children = start.children(); children.hasMoreElements();) {
    DefaultMutableTreeNode dtm = (DefaultMutableTreeNode) children.nextElement();
    if (!dtm.isLeaf() && dtm.getLevel() <= level) {
        TreePath tp = new TreePath(dtm.getPath());
        tree.expandPath(tp);
        expandTree(tree, dtm, level);
return;
voidexpandTreeLevels(JTree tree, TreePath parent, boolean expand, int desiredLevel)
Expands all tree nodes or collapse these level by level.
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);
        if (desiredLevel > 0)
            expandTreeLevels(tree, path, expand, desiredLevel - 1);
        else if (desiredLevel != 0)
...
voidexpandTreePaths(JTree tree, Enumeration expandPaths)
Used to restore expansion state of the tree like:
 Enumeration expandPaths = tree.getExpandedDescendants(pathToRoot); // do some changes in the tree model ... 
if (expandPaths != null) {
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
    while (expandPaths.hasMoreElements()) {
        TreePath curPath = expandPaths.nextElement();
        DefaultMutableTreeNode castedNode = (DefaultMutableTreeNode) curPath.getLastPathComponent();
        Object obj = castedNode.getUserObject();
        Enumeration<?> enumAll = root.breadthFirstEnumeration();
        while (enumAll.hasMoreElements()) {
...
intfullExpand(JTree tree, TreePath parentPath, int nExpansions)
full Expand
TreeNode parent = (TreeNode) parentPath.getLastPathComponent();
int count = parent.getChildCount();
for (int i = 0; i < count && nExpansions > 0; ++i) {
    TreeNode child = parent.getChildAt(i);
    TreePath childPath = parentPath.pathByAddingChild(child);
    nExpansions = fullExpand(tree, childPath, nExpansions);
tree.expandPath(parentPath);
...
CollectiongetExpandedPaths(JTree tree)
get Expanded Paths
Collection<TreePath> set = new TreeSet<TreePath>(tpLengthComparator);
TreePath root = new TreePath(tree.getModel().getRoot());
if (root != null) {
    for (Enumeration<TreePath> e = tree.getExpandedDescendants(root); e != null && e.hasMoreElements();) {
        TreePath tp = e.nextElement();
        set.add(tp);
return set;
TreePath[]getExpandedPaths(JTree tree)
Get a copy of the list of expanded tree paths of a tree.
ArrayList<TreePath> expandedPaths = new ArrayList<TreePath>();
TreePath rootPath = new TreePath(tree.getModel().getRoot());
Enumeration enumeration = tree.getExpandedDescendants(rootPath);
if (enumeration != null) {
    while (enumeration.hasMoreElements()) {
        expandedPaths.add((TreePath) enumeration.nextElement());
TreePath[] array = new TreePath[expandedPaths.size()];
expandedPaths.toArray(array);
return array;