
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Enumeration;

class MyTreeDemo extends JFrame {
    JTree tree;
    DefaultMutableTreeNode root, parent1, parent2, child,child1, child2;

    MyTreeDemo() {
        super("Debug JTree Demo");

        root = new DefaultMutableTreeNode("States");
        parent1 = new DefaultMutableTreeNode("Andhra Pradesh");
        child = new DefaultMutableTreeNode("Vijayawada");
        child1 = new DefaultMutableTreeNode("Vizag");
        parent2 = new DefaultMutableTreeNode("Telangana");
        child2 = new DefaultMutableTreeNode("Hyderabad");

        // Adding child nodes to parent
        parent1.add(child);
        parent1.add(child1);
        parent2.add(child2);
        
        // Adding parent nodes to root
        root.add(parent1);
        root.add(parent2);

        // Adding root to JTree
        tree = new JTree(root);

        getContentPane().add(new JScrollPane(tree));
        setSize(300, 300);
        setVisible(true);
        
        
        Object pathString = tree.getModel().getRoot();
        
        String root_str = root.toString();
        
        
        for(int i = 0; i < root.getChildCount() ; i++) 
        {
            DefaultMutableTreeNode child = (DefaultMutableTreeNode) root.getChildAt(i);
            String nt = child.getUserObject().toString();
            
            System.out.println(nt);
            
           
            
            
            for(int j = 0; j < child.getChildCount() ; j++) 
            {
                DefaultMutableTreeNode aaachild = (DefaultMutableTreeNode) child.getChildAt(j);
                String sub = aaachild.getUserObject().toString();
                
                System.out.println(sub);
            }
            
        }
        
        System.out.println(pathString);
        
        int n = root.getChildCount();
        
        Object blabla = ((TreeModel) root).getChild(root, 1);
        
        TreeNode parent = (TreeNode) getParent();
        
        
        
    //    int n = getChildCount(parent);

    	
        
        Enumeration children = root.children();
        
        children.nextElement();
        
        if (children != null)
        {
            while (children.hasMoreElements()) 
            {
            	System.out.println("aqw");
              //printDescendents((TreeNode) children.nextElement());
            }
        }    
        
        TreeNode root = (TreeNode)tree.getModel().getRoot();
        root.getParent();
        
        
        //((DefaultMutableTreeNode) root).getUserObject();
        
        String pipo = "123";
        
        
    }
}

class JTreeDemo {
    public static void main(String args[]) throws Exception {
        UIManager
                .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        new MyTreeDemo();
    }
}