Close

Java Swing - Prevent JTree from Collapsing

[Updated: Dec 25, 2018, Created: Dec 25, 2018]

To prevent JTree from collapsing we need to throw ExpandVetoException in TreeWillExpandListener#treeWillCollapse() method.

Quick Example:

  tree.addTreeWillExpandListener(new TreeWillExpandListener() {
       @Override
       public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
 
       }
 
       @Override
       public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
           throw new ExpandVetoException(event, "Collapsing tree not allowed");
       }
   });

Complete Example

public class ExampleMain {

  public static void main(String[] args) {
      JTree tree = new JTree(new Hashtable<>(createTreeData()));
      JTreeUtil.setTreeExpandedState(tree, true);
      JTreeUtil.makeTreeUnCollapsible(tree);
      JFrame frame = createFrame();
      frame.add(new JScrollPane(tree));
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
  }

  private static void expandNode(JTree tree, TreeNode node) {
      ArrayList<? extends TreeNode> list = Collections.list(node.children());
      for (TreeNode treeNode : list) {
          expandNode(tree, treeNode);
      }
      TreePath path = new TreePath(((DefaultMutableTreeNode) node).getPath());
      tree.expandPath(path);
  }

  private static Map<?, ?> createTreeData() {
      return Map.of("Sports",
              new String[]{"Mustang", "Corvette", "Ferrari"},
              "Luxury",
              new String[]{"BMW", "Mercedes-Benz", "Rolls-Royce"});
  }

  private static JFrame createFrame() {
      JFrame frame = new JFrame("Uncollapsible JTree");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(new Dimension(500, 400));
      return frame;
  }
}
public class JTreeUtil {
  public static void setTreeExpandedState(JTree tree, boolean expanded) {
      DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getModel().getRoot();
      setNodeExpandedState(tree, node, expanded);
  }

  public static void setNodeExpandedState(JTree tree, TreeNode node, boolean expanded) {
      ArrayList<? extends TreeNode> list = Collections.list(node.children());
      for (TreeNode treeNode : list) {
          setNodeExpandedState(tree, treeNode, expanded);
      }
      DefaultMutableTreeNode mutableTreeNode = (DefaultMutableTreeNode) node;
      if (!expanded && mutableTreeNode.isRoot()) {
          return;
      }
      TreePath path = new TreePath(mutableTreeNode.getPath());
      if (expanded) {
          tree.expandPath(path);
      } else {
          tree.collapsePath(path);
      }
  }

  public static void makeTreeUnCollapsible(JTree tree) {
      tree.addTreeWillExpandListener(new TreeWillExpandListener() {
          @Override
          public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {

          }

          @Override
          public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
              throw new ExpandVetoException(event, "Collapsing tree not allowed");
          }
      });
  }
}

Output


Example Project

Dependencies and Technologies Used:

  • JDK 11
  • Maven 3.5.4

Java Swing - Prevent JTree from Collapsing Select All Download
  • jtree-prevent-from-collapsing
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • JTreeUtil.java

    See Also