Java JTree expand all

Description

Java JTree expand all

import java.awt.Container;
import java.awt.FlowLayout;
import java.util.Collection;
import java.util.Enumeration;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public class Main {
   public static void main(String args[]) {
      final JFrame f = new JFrame("JTree Demo");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container c = f.getContentPane();
      c.setLayout(new FlowLayout());
      DefaultMutableTreeNode root = new DefaultMutableTreeNode("Computer");
      DefaultMutableTreeNode c1 = new DefaultMutableTreeNode("A");
      DefaultMutableTreeNode c2 = new DefaultMutableTreeNode("B");
      DefaultMutableTreeNode c3 = new DefaultMutableTreeNode("C");
      root.add(c1);/*  ww w.  j  ava 2 s.co  m*/
      root.add(c2);
      root.add(c3);

      c1.add(new DefaultMutableTreeNode("1"));
      c1.add(new DefaultMutableTreeNode("2"));

      c2.add(new DefaultMutableTreeNode("3"));
      c2.add(new DefaultMutableTreeNode("4"));

      c3.add(new DefaultMutableTreeNode("5"));
      DefaultMutableTreeNode n6 = new DefaultMutableTreeNode("6");

      c3.add(n6);

      JTree t = new JTree(root);

      expandAll(t, true);
      
      c.add(new JScrollPane(t));
      f.pack();
      f.setVisible(true);
   }
   /**
    * If expand is true, expands all nodes in the tree.
    * Otherwise, collapses all nodes in the tree.
    *
    * @param tree   tree
    * @param expand true if expand all nodes
    */
   public static void expandAll(JTree tree, boolean expand) {
       if (tree == null) {
           throw new NullPointerException("tree == null");
       }
       TreeNode root = (TreeNode) tree.getModel().getRoot();
       // Traverse tree from root
       expandAll(tree, new TreePath(root), expand);
   }

   /**
    * Expands or collapses a path and all it's sub paths.
    *
    * @param tree   tree
    * @param parent parent path
    * @param expand true if expand, false if collapse
    *
    */
   @SuppressWarnings("unchecked")
   public static void expandAll(JTree tree, TreePath parent, boolean expand) {
       if (tree == null) {
           throw new NullPointerException("tree == null");
       }
       if (parent == null) {
           throw new NullPointerException("parent == null");
       }
       // Traverse children
       TreeNode node = (TreeNode) parent.getLastPathComponent();

       if (node.getChildCount() >= 0) {
           for (Enumeration<TreeNode> e = node.children(); e
                   .hasMoreElements();) {
               TreeNode n = e.nextElement();
               TreePath path = parent.pathByAddingChild(n);
               expandAll(tree, path, expand);
           }
       }
       // Expansion or collapse must be done bottom-up
       if (expand) {
           tree.expandPath(parent);
       } else {
           tree.collapsePath(parent);
       }
   }

   /**
    * Expands or collapses tree paths and all it's sub paths.
    *
    * @param tree        tree
    * @param parentPaths parent paths
    * @param expand      true if expand, false if collapse
    *
    */
   public static void expandAll(JTree tree,
           Collection<? extends TreePath> parentPaths, boolean expand) {
       if (tree == null) {
           throw new NullPointerException("tree == null");
       }
       if (parentPaths == null) {
           throw new NullPointerException("parentPaths == null");
       }
       for (TreePath treePath : parentPaths) {
           expandAll(tree, treePath, expand);
       }
   }


   public static void expandPath(JTree tree, TreePath path) {
       if (tree == null) {
           throw new NullPointerException("tree == null");
       }
       if (path == null) {
           throw new NullPointerException("path == null");
       }
       TreePath expandPath = path;
       if (tree.getModel().isLeaf(path.getLastPathComponent())) {
           expandPath = path.getParentPath();
       }
       tree.expandPath(expandPath);
   }
}



PreviousNext

Related