Saturday, February 4, 2012

Tree

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 t1 = new Tree1("A Tree");
  t1.setSize(200,200);
  t1.setVisible(true);
  t1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 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));
 }
}
Output:-

No comments: