100% found this document useful (1 vote)
1K views2 pages

Java JTree Directory Example

This document contains code for two Java programs that create JTree components to display file system hierarchies. The first program displays the directories of Maharashtra and Gujarat as child nodes under the parent node "India". The second program displays the contents of the local C and D disks, showing folders like Users, Program Files, Movies etc. as child nodes under their respective parent disks. Both programs initialize the tree nodes, add the child nodes, set up the JTree component, and make the JFrame visible.

Uploaded by

rohitchavan2345
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
100% found this document useful (1 vote)
1K views2 pages

Java JTree Directory Example

This document contains code for two Java programs that create JTree components to display file system hierarchies. The first program displays the directories of Maharashtra and Gujarat as child nodes under the parent node "India". The second program displays the contents of the local C and D disks, showing folders like Users, Program Files, Movies etc. as child nodes under their respective parent disks. Both programs initialize the tree nodes, add the child nodes, set up the JTree component, and make the JFrame visible.

Uploaded by

rohitchavan2345
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/ 2

Practical -7

// write a jtree program to show root directory and its subfolder of your
sysytem
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode; public class TreeExample {
JFrame f;
TreeExample(){
f=new JFrame();
DefaultMutableTreeNode top=new DefaultMutableTreeNode("India");
DefaultMutableTreeNode a=new DefaultMutableTreeNode("Maharashtra");
DefaultMutableTreeNode b=new DefaultMutableTreeNode("Gujrat");
top.add(a);
top.add(b);
DefaultMutableTreeNode a1=new DefaultMutableTreeNode("Mumbai");
DefaultMutableTreeNode a2=new DefaultMutableTreeNode("Pune");
DefaultMutableTreeNode a3=new DefaultMutableTreeNode("Nashik");
DefaultMutableTreeNode a4=new DefaultMutableTreeNode("Nagpur");
a.add(a1);
a.add(a2);
a.add(a3);
a.add(a4);
JTree jt=new JTree(top);
f.add(jt);
f.setSize(200,200);
f.setVisible(true);
}
public static void main(String[] args) {
new TreeExample();
}}

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode; public
class TreeExample1 {
JFrame f;
TreeExample1(){
f=new JFrame();
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("User");
DefaultMutableTreeNode a2=new DefaultMutableTreeNode("Program File");
DefaultMutableTreeNode a3=new DefaultMutableTreeNode("Windows");
DefaultMutableTreeNode b1=new DefaultMutableTreeNode("Movies");
DefaultMutableTreeNode b2=new DefaultMutableTreeNode("Songs");
DefaultMutableTreeNode b3=new DefaultMutableTreeNode("Project Files");
a.add(a1);
a.add(a2);
a.add(a3);
b.add(b1);
b.add(b2);
b.add(b3);
JTree jt=new JTree(top);
f.add(jt);
f.setSize(200,200);
f.setVisible(true);

}
public static void main(String[] args) {
new TreeExample1();
}
}

You might also like