Java JTree create from DefaultMutableTreeNode

Description

Java JTree create from DefaultMutableTreeNode

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   ww  w  . j  ava  2 s. c o  m*/

      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