Ayush Practical No 7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

ADVANCE JAVA PROGRAMMING

Name :- Ayush Maroti Wadje Class : - CO5I

Roll No :- 76

Practical No 7 :- Write a Program To Create a JTree.

X. ------------ ?

1. Develop a program To Demonstrate The Use Of Tree Component In Swing.

Code :-
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class JTreeEx1 {
JTree tree;
JFrame F;
JTreeEx1() {
F=new JFrame();
F.setLayout(new GridLayout());

DefaultMutableTreeNode top=new DefaultMutableTreeNode("Fights");


DefaultMutableTreeNode a=new DefaultMutableTreeNode("Round 1");
top.add(a);
DefaultMutableTreeNode b=new DefaultMutableTreeNode("Round 2");
top.add(b);

DefaultMutableTreeNode a1=new DefaultMutableTreeNode("AMW");


a.add(a1);
DefaultMutableTreeNode a2=new DefaultMutableTreeNode("PK");
a.add(a2);

DefaultMutableTreeNode b1=new DefaultMutableTreeNode("AR");


b.add(b1);
DefaultMutableTreeNode b2=new DefaultMutableTreeNode("AK");
b.add(b2);

tree=new JTree(top);
JScrollPane jsp=new JScrollPane(tree);
F.add(jsp,BorderLayout.CENTER);
F.setSize(500,550);
F.setVisible(true);
}
public static void main(String args[]) {
JTreeEx1 jte=new JTreeEx1();
}
}

Output :-
2. write a Program Code To Generate The Following Output.

Code :-
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
class MetroCity {
JTree tree;
JFrame F;
MetroCity() {
F=new JFrame();
F.setLayout(new GridLayout());

//Createing the top Node of Tree


DefaultMutableTreeNode top=new DefaultMutableTreeNode("India");

DefaultMutableTreeNode a=new DefaultMutableTreeNode("Maharashtra");


top.add(a);

DefaultMutableTreeNode b=new DefaultMutableTreeNode("Gujrath");


top.add(b);

//Creating the subtrees of A


DefaultMutableTreeNode A1=new DefaultMutableTreeNode("Mumbai");
a.add(A1);
DefaultMutableTreeNode A2=new DefaultMutableTreeNode("Pune");
a.add(A2);
DefaultMutableTreeNode A3=new DefaultMutableTreeNode("Nashik");
a.add(A3);
DefaultMutableTreeNode A4=new DefaultMutableTreeNode("Nagpur");
a.add(A4);

tree=new JTree(top);
F.setSize(500,550);
JScrollPane jsp=new JScrollPane(tree);
F.add(jsp,BorderLayout.CENTER);
F.setVisible(true);
}
public static void main(String args[]) {
MetroCity MC1=new MetroCity();
}
}

Output :-
XII. Exercise.

Write a JTree Program To Show Root Directory And It’s subFolders Of Your
System.

Code :-
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;

public class JTreeEx extends JFrame{

JTreeEx() {
setLayout(new GridLayout());
setVisible(true);
setSize(450,400);

DefaultMutableTreeNode A = new DefaultMutableTreeNode("C Disk");


DefaultMutableTreeNode B = new DefaultMutableTreeNode("Windows");
DefaultMutableTreeNode C=new DefaultMutableTreeNode("Linux");

DefaultMutableTreeNode B1=new DefaultMutableTreeNode("Data()");


DefaultMutableTreeNode B2=new DefaultMutableTreeNode("Folder");
DefaultMutableTreeNode B3 =new DefaultMutableTreeNode("Documents");
DefaultMutableTreeNode B4=new DefaultMutableTreeNode("Media");

DefaultMutableTreeNode C1=new DefaultMutableTreeNode("Information");


DefaultMutableTreeNode C2=new DefaultMutableTreeNode("User Data");

A.add(B);
A.add(C);
B.add(B1);
B.add(B2);
B.add(B3);
B.add(B4);
C.add(C1);
C.add(C2);
JTree T1=new JTree(A);
add(T1);
}
public static void main(String args[]) {
new JTreeEx();
}
}

Output :-

You might also like