Java JTree Path setTreeState(@Nonnull final JTree tree, @Nonnull final TreePath path, final boolean recursively, final boolean unfold)

Here you can find the source of setTreeState(@Nonnull final JTree tree, @Nonnull final TreePath path, final boolean recursively, final boolean unfold)

Description

set Tree State

License

Apache License

Declaration

private static void setTreeState(@Nonnull final JTree tree, @Nonnull final TreePath path,
            final boolean recursively, final boolean unfold) 

Method Source Code

//package com.java2s;
/*/*  ww  w  .  j  a  v a  2s .c om*/
 * Copyright 2015 Igor Maznitsa.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.annotation.Nonnull;

import javax.swing.JTree;

import javax.swing.tree.TreePath;

public class Main {
    private static void setTreeState(@Nonnull final JTree tree, @Nonnull final TreePath path,
            final boolean recursively, final boolean unfold) {
        final Object lastNode = path.getLastPathComponent();
        for (int i = 0; i < tree.getModel().getChildCount(lastNode); i++) {
            final Object child = tree.getModel().getChild(lastNode, i);
            final TreePath pathToChild = path.pathByAddingChild(child);
            if (recursively) {
                setTreeState(tree, pathToChild, recursively, unfold);
            }
        }
        if (unfold) {
            tree.expandPath(path);
        } else {
            tree.collapsePath(path);
        }
    }
}

Related

  1. pathToDepth(TreePath path, int depth)
  2. resourceFromTreePath(TreePath path)
  3. restoreSelectionPath(JTree parentTree)
  4. searchForNodeV2(TreeNode node, String query, JTree tree, TreePath actualPath, boolean isRoot)
  5. selectCategoryTreePath(JTree tree, TreePath treePath)
  6. treeStringFromTreePath(TreePath path)