Java DefaultMutableTreeNode create a tree

Description

Java DefaultMutableTreeNode create a tree

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

public class Main {
   public static void main(String[] args) {

      DefaultMutableTreeNode top = new DefaultMutableTreeNode("Root");
      DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
      top.add(a);/*from   w w w  .j  a v a2s . c  om*/

      DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("1");
      a.add(a1);
      DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("2");
      a.add(a2);

      
      DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
      top.add(b);
      DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("1");
      b.add(b1);


      JTree tree = new JTree(top);

      JFrame application = new JFrame();

      application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      application.add(new JScrollPane(tree));
      application.setSize(250, 250);
      application.setVisible(true);
   }
}



PreviousNext

Related