0% found this document useful (0 votes)
23 views4 pages

Practical No 19

The document contains two Java programs: one that creates a JTree to display the root directory and its subfolders of the system, and another that creates a JTable to display the names, percentages, and grades of 10 students. The first program utilizes Swing components to build a file system tree, while the second program uses a table model to present student data. Both programs are designed to run in a graphical user interface.

Uploaded by

meghanathani16
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
0% found this document useful (0 votes)
23 views4 pages

Practical No 19

The document contains two Java programs: one that creates a JTree to display the root directory and its subfolders of the system, and another that creates a JTable to display the names, percentages, and grades of 10 students. The first program utilizes Swing components to build a file system tree, while the second program uses a table model to present student data. Both programs are designed to run in a graphical user interface.

Uploaded by

meghanathani16
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/ 4

Practical no:-19

1.Write a Jtree program to show root directory and its subFolders of your
system.
Code:-
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.io.File;
public class FileSystemTree extends JFrame
{
public FileSystemTree()
{
setTitle("File System Tree");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
File rootDir = new File("C:\\");
DefaultMutableTreeNode rootNode = new
DefaultMutableTreeNode(rootDir.getAbsolutePath());
buildTree(rootNode, rootDir);
DefaultTreeModel model = new DefaultTreeModel(rootNode);
JTree tree = new JTree(model);
JScrollPane scrollPane = new JScrollPane(tree);
add(scrollPane);
setVisible(true);
}
private void buildTree(DefaultMutableTreeNode parentNode, File directory)
{
File[] files = directory.listFiles();
if (files != null)
{
for (File file : files)
{
if (file.isDirectory())
{
DefaultMutableTreeNode childNode = new
DefaultMutableTreeNode(file.getName());
parentNode.add(childNode);
buildTree(childNode, file);
}
}
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(() -> new FileSystemTree());
}
} Output:-
2.Write a java program to create a table of Name of Student,percentage and
Grade of 10 Students using jtable.
Code:-
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
public class StudentTable extends JFrame
{

public StudentTable()
{
setTitle("Student Table");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLayout(new BorderLayout());
String[] columnNames ={"Name", "Percentage", "Grade"};
Object[][] data =
{
{"Alice", 85.5, "A"},
{"Bob", 78.0, "B"},
{"Charlie", 74.3, "C"},
{"David", 65.7, "C"},
{"Eve", 89.1, "A"},
{"Frank", 91.5, "A"},
{"Grace", 90.2, "A"},
{"Henry", 72.4, "B"},
{"Ivy", 81.2, "B"},
{"Jack", 75.8, "B"}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(() -> new StudentTable());
}
}
Output:-

You might also like