0% found this document useful (0 votes)
82 views

Practical No 7

The document contains two programs that demonstrate the use of JTree component in Swing to display hierarchical tree structures. The first program creates a tree with India as the root node and adds states of India as child nodes. The second program creates a tree with Style as the root node and adds color and font as child nodes, and different color options as child nodes of color. Both programs create JTree objects using the root nodes and add them to JFrames to display the tree structures.

Uploaded by

Pratiksha Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Practical No 7

The document contains two programs that demonstrate the use of JTree component in Swing to display hierarchical tree structures. The first program creates a tree with India as the root node and adds states of India as child nodes. The second program creates a tree with Style as the root node and adds color and font as child nodes, and different color options as child nodes of color. Both programs create JTree objects using the root nodes and add them to JFrames to display the tree structures.

Uploaded by

Pratiksha Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical No-7

1) Write a program to demonstrate the use of tree


component in swing

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
public class JTreeDemo
{
public static void main(String[] args)
{
JFrame JFrameMain = new JFrame();
JFrameMain.setVisible(true);
JFrameMain.setSize(400,400);

DefaultMutableTreeNode rootNode = new


DefaultMutableTreeNode("India");

DefaultMutableTreeNode maharashtraNode =
new DefaultMutableTreeNode("Maharashtra");

DefaultMutableTreeNode gujrathNode = new


DefaultMutableTreeNode("Gujrat");
rootNode.add(maharashtraNode);
rootNode.add(gujratNode);
DefaultMutableTreeNode mumbaiSubNode = new
DefaultMutableTreeNode("Mumbai");
DefaultMutableTreeNode puneSubNode = new
DefaultMutableTreeNode("Pune");
DefaultMutableTreeNode nashikSubNode = new
DefaultMutableTreeNode("Nashik");
DefaultMutableTreeNode nagpurSubNode = new
DefaultMutableTreeNode("Nagpur");
maharashtraNode.add(mumbaiSubNode);
maharashtraNode.add(puneSubNode);
maharashtraNode.add(nashikSubNode);
maharashtraNode.add(nagpurSubNode);
JTree tree = new JTree(rootNode);
JFrameMain.add(tree);
}
}
2) Write a program to demonstrate the use of tree
component in swing

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class TreeExample {
JFrame f;
TreeExample(){
f=new JFrame();
DefaultMutableTreeNode style=new
DefaultMutableTreeNode("Style");
DefaultMutableTreeNode color=new
DefaultMutableTreeNode("color");
DefaultMutableTreeNode font=new
DefaultMutableTreeNode("font");
style.add(color);
style.add(font);
DefaultMutableTreeNode red=new
DefaultMutableTreeNode("red");
DefaultMutableTreeNode blue=new
DefaultMutableTreeNode("blue");
DefaultMutableTreeNode black=new
DefaultMutableTreeNode("black");
DefaultMutableTreeNode green=new
DefaultMutableTreeNode("green");
color.add(red);
color.add(blue);
color.add(black);
color.add(green);
JTree jt=new JTree(style);
f.add(jt);
f.setSize(200,200);
f.setVisible(true);
}
public static void main(String[] args) {
new TreeExample();
}
}
Output:

You might also like