0% found this document useful (0 votes)
6 views3 pages

Ajp Prac7

The document contains Java code for two applets that utilize JTree to display hierarchical data. The first applet creates a tree structure representing geographical locations in India, while the second example demonstrates how to create a JTree that lists files in the user's home directory. Both examples showcase the use of DefaultMutableTreeNode and JFrame for GUI representation.
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)
6 views3 pages

Ajp Prac7

The document contains Java code for two applets that utilize JTree to display hierarchical data. The first applet creates a tree structure representing geographical locations in India, while the second example demonstrates how to create a JTree that lists files in the user's home directory. Both examples showcase the use of DefaultMutableTreeNode and JFrame for GUI representation.
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/ 3

X

1.import java.awt.*; 2.import java.awt.*;


import java.applet.*; import java.applet.*;
import javax.swing.tree.*; import javax.swing.tree.*;
import javax.swing.*; import javax.swing.*;
import java.util.*; import java.util.*;
import java.net.*; import java.net.*;
/*<applet code="tree1.class" width=300 /*<applet code="tree1.class" width=300
height=400> </applet> */ height=400> </applet> */
public class tree1 extends JApplet { public class tree1 extends JApplet{
public void init() { public void init()
Container c1 = getContentPane(); {
DefaultMutableTreeNode top = new Container c1= getContentPane();
DefaultMutableTreeNode("TOP"); DefaultMutableTreeNode India=new
DefaultMutableTreeNode A = new DefaultMutableTreeNode("India");
DefaultMutableTreeNode("A"); DefaultMutableTreeNode Maharashtra=new
DefaultMutableTreeNode B = new DefaultMutableTreeNode("Maharashtra");
DefaultMutableTreeNode("B"); DefaultMutableTreeNode Gujarat=new
DefaultMutableTreeNode C = new DefaultMutableTreeNode("Gujarat");
DefaultMutableTreeNode("C"); DefaultMutableTreeNode Mumbai=new
DefaultMutableTreeNode a = new DefaultMutableTreeNode("Mumbai");
DefaultMutableTreeNode("a"); DefaultMutableTreeNode Pune=new
DefaultMutableTreeNode b = new DefaultMutableTreeNode("Pune");
DefaultMutableTreeNode("b"); DefaultMutableTreeNode Nashik=new
DefaultMutableTreeNode c = new DefaultMutableTreeNode("Nashik");
DefaultMutableTreeNode("c"); DefaultMutableTreeNode Nagpur=new
top.add(A); top.add(B);top.add(C); DefaultMutableTreeNode("Nagpur");
A.add(a); B.add(b); C.add(c); India.add(Maharashtra);
JTree jt = new JTree(top); India.add(Gujarat);
c1.add(jt);} Maharashtra.add(Mumbai);
public void paint(Graphics g) { Maharashtra.add(Pune);
super.paint(g); Maharashtra.add(Nashik);
Date d = new Date(); Maharashtra.add(Nagpur);
try{ JTree jt=new JTree(India);
InetAddress ip = c1.add(jt);
InetAddress.getLocalHost(); }
String year = d.getYear()+1900+""; public void paint(Graphics g) {
showStatus("Performed by akshay & prajol super.paint(g);
on"+d.getDate()+"/"+d.getMonth()+"/"+year+" Date d = new Date();
with address "+ip); try{
}catch(Exception e){ InetAddress ip =
e.printStackTrace(); InetAddress.getLocalHost();
} String year = d.getYear()+1900+"";
}} showStatus("Performed by akshay & prajol
on"+d.getDate()+"/"+d.getMonth()+"/"+year+"
with address "+ip);
}catch(Exception e){
e.printStackTrace();
}}}
XIII.

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.io.File;
import java.util.*;
import java.net.*;

public class JTreeExample {


public static void main(String[] args) {
JFrame frame = new JFrame("JTree Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTree tree = new JTree();


DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root Directory");
File rootDir = new File(System.getProperty("user.home"));
File[] files = rootDir.listFiles();

if (files != null) {
for (File file : files) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(file.getName());
root.add(node);
if (file.isDirectory()) {
addFilesToNode(file, node);
}
}
}
DefaultTreeModel model = new DefaultTreeModel(root);
tree.setModel(model);
frame.getContentPane().add(new JScrollPane(tree));
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);

frame.setVisible(true);
}

private static void addFilesToNode(File dir, DefaultMutableTreeNode node) {


File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(file.getName());
node.add(newNode);
if (file.isDirectory()) {
addFilesToNode(file, newNode);
}
}
}
}
}

You might also like