0% found this document useful (0 votes)
3 views6 pages

AJP7

The document contains Java programs demonstrating the use of JTree components in Swing for creating tree structures. It includes examples for displaying a tree of cricketers, a geographical hierarchy of India, and a representation of system folders and subdirectories. Each program is accompanied by code snippets and descriptions of their functionality.

Uploaded by

sumedhr56
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)
3 views6 pages

AJP7

The document contains Java programs demonstrating the use of JTree components in Swing for creating tree structures. It includes examples for displaying a tree of cricketers, a geographical hierarchy of India, and a representation of system folders and subdirectories. Each program is accompanied by code snippets and descriptions of their functionality.

Uploaded by

sumedhr56
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/ 6

dd

NAME: SUMEDH SANJAY RAUT


CLASS: CO5I(A)
ROLL NO: 532
SUBJECT: AJP(22517)

 PRATICAL NO :- 7

Q1.Java program to demonstrate use of tree component in swing.


CODE:-

import javax.swing.*;
import javax.swing.tree.*;
public class JtreeEx1 {
public static void main(String[] args) {
DefaultMutableTreeNode root = new
DefaultMutableTreeNode("CRICKETER");
DefaultMutableTreeNode child1 = new
DefaultMutableTreeNode("SANKET");
DefaultMutableTreeNode child2 =
newDefaultMutableTreeNode("ROHIT");
DefaultMutableTreeNode child3 = new
DefaultMutableTreeNode("SURYA");

root.add(child1);
root.add(child2);
root.add(child3);
JTree tree = new JTree(root);
JFrame frame = new JFrame("JTree Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(tree));
frame.setSize(300, 300);
frame.setVisible(true);
}
}
dd

OUTPUT:-

Q2. Write a program code to generate the following output


Code:-
import javax.swing.*;
import javax.swing.tree.*;
public class JtreeEx2 {
public static void main(String[] args) {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("India");
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Maharastra");
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Mumbai");
DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("Pune");
DefaultMutableTreeNode child4 = new DefaultMutableTreeNode("Nashik");
DefaultMutableTreeNode child5 = new DefaultMutableTreeNode("Nagpur");
DefaultMutableTreeNode child6 = new DefaultMutableTreeNode("Gujrat");
root.add(child1);
root.add(child6);
child1.add(child2);
child1.add(child3);
child1.add(child4);
child1.add(child5);
JTree tree = new JTree(root);
JFrame frame = new JFrame("JTree Example");
dd

frame.setDefaultCloseOperation(JFrame.EXIT_
ON_CLOSE); frame.add(new
JScrollPane(tree));
frame.setSize(300, 300);
frame.setVisible(true);
}
}
Output:-
dd

Q1. Write a Jtree program to show root directory and its subdirectory and its sub
Folders of Your System.
Code:-

import javax.swing.*;

import javax.swing.tree.*;

public class JtreeEx3 {

public static void main(String[] args) {

DefaultMutableTreeNode root = new DefaultMutableTreeNode("System Folders and subdirectories");

DefaultMutableTreeNode child0 = new DefaultMutableTreeNode("D drive");

DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("jdk");

DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Ubuntu");

DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("photo");

DefaultMutableTreeNode child4 = new

DefaultMutableTreeNode("Screenhot");

DefaultMutableTreeNode child5 = new DefaultMutableTreeNode("Images");

DefaultMutableTreeNode child6 = new

DefaultMutableTreeNode("Desktop");

DefaultMutableTreeNode child7 = new DefaultMutableTreeNode("JS Folder");

DefaultMutableTreeNode child8 = new DefaultMutableTreeNode("JS program");

DefaultMutableTreeNode child9 = new DefaultMutableTreeNode("Js Project");

DefaultMutableTreeNode child10 = new DefaultMutableTreeNode("zoom");

DefaultMutableTreeNode child11 = new DefaultMutableTreeNode("C drive");

DefaultMutableTreeNode child12 = new DefaultMutableTreeNode("Apps");

DefaultMutableTreeNode child13 = new

DefaultMutableTreeNode("Whatsapp");

DefaultMutableTreeNode child14 = new DefaultMutableTreeNode("Subway Surfer");

DefaultMutableTreeNode child15 = new


dd

DefaultMutableTreeNode("Movies");

root.add(child0);

child0.add(child1);

child0.add(child2);

child0.add(child3);

child3.add(child4);

child3.add(child5);

root.add(child6);

child6.add(child7);

child7.add(child8);

child7.add(child9);

child6.add(child10);

root.add(child11);

child11.add(child12);

child12.add(child13);

child12.add(child14);

child11.add(child15);

JTree tree = new

JTree(root);

JFrame frame = new JFrame("JTree Example");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(new

JScrollPane(tree));

frame.setSize(300, 300);

frame.setVisible(true);

}
dd

Output:-

You might also like