Java Program To Display a Simple Tree

1 comments - Post a comment

/* Java Program To Display A simple tree with root node as ENGINEERING and the courses under it as sub nodes or leaves. The program makes use of Frames. The Normal way of defining a tree node is 'DefaultMutableTreeNode root_name =new DefaultMutableTreeNode (argument_name)'.*/


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

public class Tree1 extends JFrame
{
public static void main(String args[])
{
Tree1 frame = new Tree1(" A Tree");
frame.setSize(200,200);
frame.setVisible(true);
}
public Tree1(String title)
{
setTitle(title);
DefaultMutableTreeNode root=new DefaultMutableTreeNode ("Engineering");
DefaultMutableTreeNode style=new DefaultMutableTreeNode ("Information Technology");
root.add(style);
style=new DefaultMutableTreeNode("Electronics");
root.add(style);
style=new DefaultMutableTreeNode ("Computer Science");
root.add(style);
style=new DefaultMutableTreeNode ("Mechanical");
root.add(style);
style=new DefaultMutableTreeNode ("Electrical");
root.add(style);
style=new DefaultMutableTreeNode ("Sound");
root.add(style);

JTree jt=new JTree(root);
Container contentPane=getContentPane();
contentPane.add(new JScrollPane(jt));
}
}

 
This Post has 1 Comment Add your own!
Anonymous - March 16, 2012 at 5:53 PM

THANKS. IT HELP ME AS MY GUIDE IN CREATING A TREES BY USING 10 ARRAY NAMES, AND HAVING SUBTREES.

Post a Comment