I need help with a Java Directory Tree assignment. Here is the assignment: In th
ID: 3558771 • Letter: I
Question
I need help with a Java Directory Tree assignment.
Here is the assignment:
In this assignment, students are required to implement an operating system command that displays the tree structure of a folder or directory. The program ask the user to enter a folder name. Next, the program traverses teh tree hiearchy of this folder in preorder traversal to retrieve the children folders or files in the specified folder. As the hierarchy is being traversed, the names of the subfolders or files are displayed in an indented fashion where the amount of identation reflects the hiearchy level of the displayed subfolders or files. This command should produce an output similar to the tree command in the DOS operating system.
Explanation / Answer
import java.io.File; import java.util.Iterator; import java.util.Vector; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.JTextArea; import javax.swing.JTree; import javax.swing.event.TreeModelEvent; import javax.swing.event.TreeModelListener; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.TreeModel; import javax.swing.tree.TreePath; public class FileTreeFrame extends JFrame { private JTree fileTree; private FileSystemModel fileSystemModel; private JTextArea fileDetailsTextArea = new JTextArea(); public FileTreeFrame(String directory) { super("JTree FileSystem Viewer"); fileDetailsTextArea.setEditable(false); fileSystemModel = new FileSystemModel(new File(directory)); fileTree = new JTree(fileSystemModel); fileTree.setEditable(true); fileTree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent event) { File file = (File) fileTree.getLastSelectedPathComponent(); fileDetailsTextArea.setText(getFileDetails(file)); } }); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, new JScrollPane( fileTree), new JScrollPane(fileDetailsTextArea)); getContentPane().add(splitPane); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(640, 480); setVisible(true); } private String getFileDetails(File file) { if (file == null) return ""; StringBuffer buffer = new StringBuffer(); buffer.append("Name: " + file.getName() + " "); buffer.append("Path: " + file.getPath() + " "); buffer.append("Size: " + file.length() + " "); return buffer.toString(); } public static void main(String args[]) { new FileTreeFrame("c:\"); } } class FileSystemModel implements TreeModel { private File root; private Vector listeners = new Vector(); public FileSystemModel(File rootDirectory) { root = rootDirectory; } public Object getRoot() { return root; } public Object getChild(Object parent, int index) { File directory = (File) parent; String[] children = directory.list(); return new TreeFile(directory, children[index]); } public int getChildCount(Object parent) { File file = (File) parent; if (file.isDirectory()) { String[] fileList = file.list(); if (fileList != null) return file.list().length; } return 0; } public boolean isLeaf(Object node) { File file = (File) node; return file.isFile(); } public int getIndexOfChild(Object parent, Object child) { File directory = (File) parent; File file = (File) child; String[] children = directory.list(); for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.