I want to create a very very simple file manager with JTree, but I only saw a very hard code and I want to create this script very clean and simple.
Can you help me? How do I List the directories of my computer in JTree?
I want to create a very very simple file manager with JTree, but I only saw a very hard code and I want to create this script very clean and simple.
Can you help me? How do I List the directories of my computer in JTree?
I wrote the simplest example of a file browser I could think of. It lists all of the directories and files on the C: drive on a Windows computer.
Here's the result.
And here's the code. I put everything together in one class to make it easier to paste here. You should separate the classes.
package com.ggl.testing;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class FileBrowser implements Runnable {
private DefaultMutableTreeNode root;
private DefaultTreeModel treeModel;
private JTree tree;
@Override
public void run() {
JFrame frame = new JFrame("File Browser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
File fileRoot = new File("C:/");
root = new DefaultMutableTreeNode(new FileNode(fileRoot));
treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
tree.setShowsRootHandles(true);
JScrollPane scrollPane = new JScrollPane(tree);
frame.add(scrollPane);
frame.setLocationByPlatform(true);
frame.setSize(640, 480);
frame.setVisible(true);
CreateChildNodes ccn =
new CreateChildNodes(fileRoot, root);
new Thread(ccn).start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new FileBrowser());
}
public class CreateChildNodes implements Runnable {
private DefaultMutableTreeNode root;
private File fileRoot;
public CreateChildNodes(File fileRoot,
DefaultMutableTreeNode root) {
this.fileRoot = fileRoot;
this.root = root;
}
@Override
public void run() {
createChildren(fileRoot, root);
}
private void createChildren(File fileRoot,
DefaultMutableTreeNode node) {
File[] files = fileRoot.listFiles();
if (files == null) return;
for (File file : files) {
DefaultMutableTreeNode childNode =
new DefaultMutableTreeNode(new FileNode(file));
node.add(childNode);
if (file.isDirectory()) {
createChildren(file, childNode);
}
}
}
}
public class FileNode {
private File file;
public FileNode(File file) {
this.file = file;
}
@Override
public String toString() {
String name = file.getName();
if (name.equals("")) {
return file.getAbsolutePath();
} else {
return name;
}
}
}
}
This is how I solved the problem when i encountered it for my project which involved a jtree to show results of my scan.
scanner function is used to initialise the parameters and call the displayDirectoryContents function
Here are some drawbacks of the method i used
The model should be manually cleared by right clicking on the properties and clearing the model
Function used
public void displayDirectoryContents(File dir,DefaultMutableTreeNode root2) throws InterruptedException
{
DefaultMutableTreeNode newdir = new DefaultMutableTreeNode();
File[] files = dir.listFiles(); //creates array of file type for all the files found
for (File file : files)
{
if(file == null)
{
System.out.println("NUll directory found ");
continue;
}
if (file.isDirectory())
{
//file is a directory that is a folder has been dound
if (file.listFiles()==null)
{
//skips null files
continue;
}
//gets the current model of the jtree
DefaultTreeModel model =(DefaultTreeModel) result.getModel();
//gets the root
DefaultMutableTreeNode root=(DefaultMutableTreeNode) model.getRoot();
//generates a node newdir using filename
newdir = new DefaultMutableTreeNode(file.getName());
//adds a node to the root of the jtree
root2.add(newdir);
//refresh the model to show the changes
model.reload();
//recursively calls the function again to explore the contents folder
displayDirectoryContents(file,newdir);
}
else
{
// Else part File is not a directory
//gets the current model of the tree
DefaultTreeModel model =(DefaultTreeModel) result.getModel();
//selected node is the position where the new node will be inserted
DefaultMutableTreeNode selectednode = root2;
DefaultMutableTreeNode newfile =new DefaultMutableTreeNode(file.getName());
//inserts a node newfile under selected node which is the root
model.insertNodeInto(newfile, selectednode, selectednode.getChildCount());
//refresh the model to show the changes
model.reload();
}
}
}
Scanner Function to initialise the above function
public void scanner() throws InterruptedException {
// creates a file with the location filename
String location ="C:\\Users\\Ashish Padalkar\\Documents";
File currentDir = new File(location);
//result is the variable name for jtree
DefaultTreeModel model =(DefaultTreeModel) result.getModel();
//gets the root of the current model used only once at the starting
DefaultMutableTreeNode root=(DefaultMutableTreeNode) model.getRoot();
//function caled
displayDirectoryContents(currentDir,root);
}
This is my First answer on stack overflow :)