3 Dead Simple Ways to Create a JTree

Screenshot of JTable Example
JTree, a funny, advanced and a terrific component. But why is it funny? It looks like that for me, for you? Advanced? Yes, because I didn't see it in the AWT. Terrific? Hmm, the name isn't but there is a minister DefaultMutableTreeNode at its back without which JTree would be nothing more than a seed that is. OMG!! See how the name is?? What is mutable? What is node? What's all this? Feel like skimming down to program! Stop, especially if you are a starter! Don't even close the tab!! Haha!

First of all, a DefaultMutableTreeNode is a class. Consider it as a cell in a JTree, otherwise consider it is a branch of a tree. A branch may nor may not have sub-branches. Similarly, a node may nor may not have sub-nodes (a.k.a. children). See the below, diagram. I'm sure, everything will be clear.

Explaining DefaultMutableTreeNode - Root, Parents, Leaves

Consider an analogy, ROOT is the oldest one in the family say your Grandfather who has three children called colors, sports and food. You are Mr. Blue (say for example) and your siblings are violet, red and yellow. sports and food are your uncles and they have their own children. Neither you nor your siblings nor your cousins (basketball,soccer,hotdogs....) have children. So you are called leaves. But your father and his brothers have, so they are parents of leaves, but they ultimately have one father, the oldest one in the family is the root.


 import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;
class JTreeExample extends JFrame
{
JTree jt1,jt2,jt3;
String[] data={"Item 1","Item 2","Item 3","Item 4","Item 5"};
DefaultMutableTreeNode parent,i1,i2,i3,i4,i5;
JScrollPane js1,js2,js3;

    public JTreeExample()
    {
        createAndShowGUI();
    }
  
    private void createAndShowGUI()
    {
        setTitle("JTree Example");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridLayout(2,3));
      
        jt1=new JTree();
      
        js1=new JScrollPane(jt1);
      
        add(js1);
      
        // Without any root but given data
        jt2=new JTree(data);
      
        // Create JScrollPane for jt2
        js2=new JScrollPane(jt2);
      
        // add js2
        add(js2);
      
        // The parent node contains children (aka sub-nodes)
        parent=new DefaultMutableTreeNode("Parent node");
      
        // These are good, they love children but they
        // haven't
        // They are just leaves, they don't have sub-nodes (a.k.a children)
        i1=new DefaultMutableTreeNode("Item 1!");
        i2=new DefaultMutableTreeNode("Item 2!");
        i3=new DefaultMutableTreeNode("Item 3!");
        i4=new DefaultMutableTreeNode("Item 4!");
      
        // If false, i5 doesn't allow children, may be
        // it did family planning operation!! ;)
        // default is true
        i5=new DefaultMutableTreeNode("Final Item",false);
      
        // Add a sub item to i1
        i1.add(new DefaultMutableTreeNode("Sub item 1"));
      
        // Add children to parents
        parent.add(i1);
        parent.add(i2);
        parent.add(i3);
        parent.add(i4);
        parent.add(i5);
      
        // A jtree with parent
        // Here parent is only one, he is root too!!
        jt3=new JTree(parent);
      
        js3=new JScrollPane(jt3);
      
        add(js3);
      
        setSize(400,400);
        setVisible(true);
    }
  
    public static void main(String args[])
    {
        new JTreeExample();
    }
}

The greatest compliment you can give me is when you share this with others. I sincerely appreciate it :)

“Always do what you are afraid to do.” - Ralph Waldo Emerson


Posted by at on

Tags: Swing Components,