I have a question which could be language-agnostic but for this particular implementation I’m using Java. It is possible and relatively trivial to list the folders in a directory – using a function like this:
private DefaultMutableTreeNode GenerateFSTree(File f)
{
int i = 0;
File[] Children = f.listFiles();
DefaultMutableTreeNode x = new DefaultMutableTreeNode(f.getName());
if ( Children != null )
{
for ( i = 0; i < Children.length; i++ )
{
File f_cur = Children[i];
if (
f_cur.isDirectory() &&
( this.DisplayHidden || !f_cur.isHidden() )
)
{
x.add(GenerateFSTree(f_cur));
}
}
}
return x;
}
As you can see this makes heavy use of recursion to evaluate the filesystem and what you end up with is a tree of DefaultMutableTreeNode items.
Now my question is – is there a faster way to do this? There must be, because this is slow. Try executing this on / and it takes forever. Yet if I use say Nautilus or the built-in Java File selection dialog, the tree renders instantly.
So my question is – how can I speed this up?
Thanks
take a look at this example
http://www.java2s.com/Code/Java/Swing-JFC/FileTree.htm
this treemodel will provide the filesystem structure to your jtree. the file system is only accessed when expanding a node… and initially for the root node. 😉
usage
this model will prevent you from filling up your memory with DefaultTreeNodes and minimizes the access to the underlying filesystem.