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

Java Practical7

Uploaded by

Priyanka Thadke
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java Practical7

Uploaded by

Priyanka Thadke
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Name:- Priyanka Khandu Thadke

Class:-A(CO5I) Roll no:- 60


Practical no:-07 Write a java to Create a JTree.
Program code:-

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


import javax.swing.*;

import javax.swing.tree.*;

public class JtreeExample {

public static void main(String[] args) {

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Thadke");

DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Priyanka");

DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Ankita");

DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("Sayali");

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

Output:
Q2. Write a program code to generate the following output
import javax.swing.*;

import javax.swing.tree.*;

public class JtreeExample {

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

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(new JScrollPane(tree));

frame.setSize(300, 300);

frame.setVisible(true);

Output:

Exercise:

Q1. Write a Jtree program to show root directory and its subdirectory and its
subFolders of Your System.
import javax.swing.*;

import javax.swing.tree.*;

public class JtreeExample {

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("Oracle.exe");

DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("photo");

DefaultMutableTreeNode child4 = new DefaultMutableTreeNode("Screenhot");

DefaultMutableTreeNode child5 = new DefaultMutableTreeNode("Images");

DefaultMutableTreeNode child6 = new DefaultMutableTreeNode("Desktop");

DefaultMutableTreeNode child7 = new DefaultMutableTreeNode("Ajp folder");

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

DefaultMutableTreeNode child9 = new DefaultMutableTreeNode("swing program");

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

DefaultMutableTreeNode child15 = new DefaultMutableTreeNode("Avm");

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

Output:

You might also like