Tree

To create three component int Java swing we use JTree class of the package javax.swing used to create the tree. Using tree class you can create the hierarchical view of items.

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


class SwingDemo
{
    public static void main(String ar[])
    {
        JFrame f=new JFrame("Tool bar demo");
        
        DefaultMutableTreeNode a=new DefaultMutableTreeNode("Info");
        DefaultMutableTreeNode name=new DefaultMutableTreeNode("Name");
        DefaultMutableTreeNode phone=new DefaultMutableTreeNode("Phone");
        DefaultMutableTreeNode address=new DefaultMutableTreeNode("Address");
        
        DefaultMutableTreeNode n1=new DefaultMutableTreeNode("abc xyz");
        DefaultMutableTreeNode n2=new DefaultMutableTreeNode("Abc ab");
        DefaultMutableTreeNode a1=new DefaultMutableTreeNode("Ab jkh mkjsdfk");
        DefaultMutableTreeNode a2=new DefaultMutableTreeNode("fsd dklsj kl");
        
        a.add(name);
        a.add(phone);
        a.add(address);
        
        name.add(n1);
        name.add(n2);
        
        address.add(a1);
        address.add(a2);
        
        JTree tree=new JTree(a);
        f.add(tree);
        
        f.setBounds(100,100,400,300);
        f.setVisible(true);
    }
}


No comments:

Popular Posts