Close

Java Swing - Understanding TreeNode and Creating JTree from DefaultMutableTreeNode

[Last Updated: Feb 16, 2018]

In this tutorial we will create JTree from DefaultMutableTreeNode.

DefaultMutableTreeNode is an implementation of MutableTreeNode which is a subinterface of TreeNode.

Understanding TreeNode

A TreeNode instance represents an object which can have multiple children nodes but has only one parent node. The root node has null parent. This interface has only getters to get information about this or children or parent nodes, thus it represents an immutable node.

Understanding MutableTreeNode

This is a subinterface of TreeNode, which represents a mutable node. This interface additionally introduces methods to insert and remove nodes.

DefaultMutableTreeNode is an implementation of MutableTreeNode which represents a general-purpose node.

Example

Creating JTree

public class TreeExampleMain {
  public static void main(String[] args) {
      TreeNode projectHierarchyTreeNode =
              TradingProjectDataService.instance.getProjectHierarchy();
      JTree tree = new JTree(projectHierarchyTreeNode);
      tree.setCellRenderer(new TradingProjectTreeRenderer());
      JFrame frame = createFrame();
      frame.add(new JScrollPane(tree));
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
  }

  private static JFrame createFrame() {
      JFrame frame = new JFrame("JTree Renderer example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(new Dimension(500, 400));
      return frame;
  }
}

Using DefaultMutableTreeNode

public enum TradingProjectDataService {
  instance;
  private final String ROLES[] =
          {"Project Manager", "Tech Lead", "Developer", "Scrum Master", "Business Analyst"};
  private DefaultMutableTreeNode rootNode;

  TradingProjectDataService() {
      rootNode = new DefaultMutableTreeNode("Trading Project Modules");
      addModule("Trading", "Real Time Trading", "Order System");
      addModule("Future/Option", "Option Analyzer", "Market Scanning System");
      addModule("Fixed Income", "Bond Tool", "Price/Yield Calculator",
              "Strategy Evaluator");
  }

  private void addModule(String module, String... projects) {
      DefaultMutableTreeNode moduleNode = new DefaultMutableTreeNode(module);
      rootNode.add(moduleNode);
      for (String project : projects) {
          moduleNode.add(getProjectNode(module, project));
      }
  }

  private MutableTreeNode getProjectNode(String module, String project) {
      DefaultMutableTreeNode projectNode = new DefaultMutableTreeNode(new Project(project));
      for (int i = 0; i < ROLES.length; i++) {
          projectNode.add(getEmployeeNodeForRole(module, project, ROLES[i]));
      }
      return projectNode;
  }

  private MutableTreeNode getEmployeeNodeForRole(String module, String project, String role) {
      //just a random employee for testing
      ProjectParticipant projectParticipant = new ProjectParticipant(RandomUtil.getFullName(), role);
      DefaultMutableTreeNode employeeNode = new DefaultMutableTreeNode(projectParticipant);
      return employeeNode;
  }

  public TreeNode getProjectHierarchy() {
      return rootNode;
  }
}

The rest of the classes are same as our last example's classes.

Output

Output is same as our last example's output.

Example Project

Dependencies and Technologies Used:

  • JDK 1.8
  • Maven 3.3.9

DefaultMutableTreeNode Example Select All Download
  • default-mutable-tree-node-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • TradingProjectDataService.java
                • util
          • resources
            • images

    See Also