0% found this document useful (0 votes)
34 views2 pages

Practical No 7

The document contains code for creating a JTree in Java. It imports necessary packages and classes. It creates a JFrame and sets its properties. It then creates DefaultMutableTreeNode objects to represent nodes in the tree with labels like "India", "Maharashtra" etc to create a hierarchical tree structure. Child nodes are added to parent nodes. Finally a JTree is instantiated with the root node and added to the JFrame to display the tree.

Uploaded by

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

Practical No 7

The document contains code for creating a JTree in Java. It imports necessary packages and classes. It creates a JFrame and sets its properties. It then creates DefaultMutableTreeNode objects to represent nodes in the tree with labels like "India", "Maharashtra" etc to create a hierarchical tree structure. Child nodes are added to parent nodes. Finally a JTree is instantiated with the root node and added to the JFrame to display the tree.

Uploaded by

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

Practical No 7

import javax.swing.*;

import javax.swing.tree.*;

import java.awt.*;

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("Gujrath");

rootNode.add(maharashtraNode);

rootNode.add(gujrathNode);

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);

}
Practical No 7
import javax.swing.*;

import javax.swing.tree.*;

import java.awt.*;

class JTreeDemo1

public static void main(String[] args) {

JFrame JFrameMain = new JFrame();

JFrameMain.setVisible(true);

JFrameMain.setSize(400,400);

DefaultMutableTreeNode top = new DefaultMutableTreeNode("Computer");

DefaultMutableTreeNode a = new DefaultMutableTreeNode("Local Disk(C:)");

DefaultMutableTreeNode b = new DefaultMutableTreeNode("Local Disk(D:)");

top.add(a);

top.add(b);

DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("Intel");

DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("Program files");

DefaultMutableTreeNode a3 = new DefaultMutableTreeNode("User");

DefaultMutableTreeNode a4 = new DefaultMutableTreeNode("Windows");

a.add(a1);

a.add(a2);

a.add(a3);

DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("Project files");

DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("song");

b.add(b1);

b.add(b2);

JTree tree = new JTree(top);

JFrameMain.add(tree);

You might also like