I have hierarchy of File structure in Java.. How do I convert that to a DefaultMutableTreeNode heirarchy. I tried fetching each file individually, checked each if directory or not, did a recursive loop and formed a File structure. Now, to convert it to DefaultMutableTreeNode, do we have any utilities or its just again we need to do top-down approach to check and convert them again node-by-node.?
Do we have any utilities as such already?
Need suggestions!
There are no built-in utilities for create a JTree model from a filesystem snippet. There is a file chooser if that is what you are after. Otherwise I would say that your approach seems reasonable to me. I would probably convert the files to a DefaultMutableTreeNode in one pass. Something like:
public class FileNode extends DefaultMutableTreeNode {
private File file;
public FileWrapper(File file) {
super(this);
this.file = file;
if(file.isDirectory()) {
for(File file : listFiles) {
add(new FileNode(file));
}
}
}
private String toString() {
return file.getName();
}
private File getFile() {
return file;
}
}