Create A JTree The Easy Way Using Java

I forgot where I got this code but I am posting it here for others who may want to have an easy way to use a JTree. Rather than doing lines of codes for adding nodes, you can use this class (I call it EasyJTree) to add notes using String names. Here is the code.

To use the EasyJTree class is pretty easy. Note that you must always supply the root node’s String value as “root”. Do not worry. The class’ root node is set to hidden by default but the code needs a root node so it can form the tree hierarchy properly.

Related Posts Plugin for WordPress, Blogger...

5 comments

  1. The leaf node is just a parent node with no children (when you expand it, nothing is there). However, I solved it by adding this:

    //get two nodes
    DefaultMutableTreeNode parentNode = getNode(parentNodeName);
    DefaultMutableTreeNode childNode = getNode(childNodeName);

    if(!parentNodeName.equals(“root”)){
    childNode.setAllowsChildren(false);
    }

    //Insert nodes into tree

    And it worked :), maybe you might want to modify the code
    Thanks anyway

Leave a Reply

Your email address will not be published. Required fields are marked *