Thursday, 29 January 2015

Tutorial on JTree in Java

A tree is a component that presents a hirerachical view of data.It does not contain a data its only it only provide how data will view.A tree has the ability to expand or collapse.Trees are implemented in  Swing  by JTree class,which expand JComponent.Some of its constructor are shown below

CONSTRUCTORS OF JTREE:

JTree()
JTree(TreeNode)
JTree(TreeNode,boolean)
JTree(Hashtable ht)

JTree(Object obj[])
JTree(Vector v)


DEFAULT MUTABLE  TREE  NODE:

Default mutable may have zero or more than one children nodes.It provides all operations for examining and modifying tree.

CODING:


import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;
 class myjframe1 extends JFrame 
{
JTree tree;
    public myjframe1()
 {
   
setTitle("Simple example");
setSize(300, 200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);      
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Names  of   OS");
//creating subtree of "A"
 DefaultMutableTreeNode a = new DefaultMutableTreeNode("  Windows   8");
top.add(a);
DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("Solaris");
a.add(a1);
DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("Mac");
a.add(a2);
DefaultMutableTreeNode a3 = new DefaultMutableTreeNode("Solaris");
a.add(a3);
//creating subtree of "B"
 DefaultMutableTreeNode b = new DefaultMutableTreeNode(" Names  of  Browser  ");
top.add(b);
DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("Google chrome");
b.add(b1);
DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("Internet Explorer");
b.add(b2);
DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("Monzila");
b.add(b3);
setLayout(new FlowLayout(FlowLayout.CENTER));
tree = new JTree(top);
add(tree);

  }
}
class tree
{
public static void main(String args[])
{
myjframe1 x= new myjframe1();
}
}

OUTPUT:


No comments:

Post a Comment