[Help] Viewing filesystem with JTree

A

artur_spruce

Hi,

I know this subject has been discussed here before, but I don't want to
deal with tree models etc. I built a custom tree node (code at end) and
hooked it to a JTree like this:

pathTree = new JTree(new FileTreeNode("/"));

but it only displays the root node and then all the first-level files
and directories as leaves. How do I make them display as expandable
nodes? Apparently none of the getChildCount(), getAllowsChildren(), or
isLeaf() help.

(I know my implementation of the node is a bit naive as far as
synchronization and order.)

Or maybe I can't do it this way?

TIA,

AdSR

---- FileTreeNode ----
package fileviewer;

import java.io.File;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.tree.TreeNode;

public class FileTreeNode implements TreeNode {
private TreeNode parent;
private File file;
private Hashtable children;

public FileTreeNode(String path) {
this(path, null);
}

public FileTreeNode(String path, TreeNode parent) {
file = new File(path);
children = new Hashtable();
}

public TreeNode getChildAt(int childIndex) {
String[] names = file.list();
String name = names[childIndex];
if (children.containsKey(name))
return (TreeNode)children.get(name);
FileTreeNode child = new FileTreeNode(name, this);
children.put(name, child);
return child;
}

public int getChildCount() {
if (file.isDirectory()) {
return file.list().length;
}
return 0;
}

public TreeNode getParent() {
return parent;
}

public int getIndex(TreeNode node) {
String name = ((FileTreeNode)node).file.getName();
String[] names = file.list();
for (int i=0; i<names.length; i++)
if (names.equals(name))
return i;
return -1;
}

public boolean getAllowsChildren() {
return file.isDirectory();
}

public boolean isLeaf() {
return !file.isDirectory();
}

public Enumeration children() {
return new ChildrenEnumeration();
}

public String toString() {
return file.getName();
}

private class ChildrenEnumeration implements Enumeration {
String[] names;
int index;

public ChildrenEnumeration() {
names = file.list();
}

public boolean hasMoreElements() {
return index < names.length - 1;
}

public Object nextElement() {
String name = names[index];
index++;
if (children.containsKey(name))
return children.get(name);
FileTreeNode child = new FileTreeNode(name, FileTreeNode.this);
children.put(name, child);
return child;
}
}
}
 
A

artur_spruce

Duh... As often, this was a simple mistake: No paths were taken into
account. So the class should look like (tested):

package fileviewer;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.tree.TreeNode;

public class FileTreeNode implements TreeNode {
private TreeNode parent;
private File file;
private Hashtable children;

public FileTreeNode(String path) {
this(path, null);
}

public FileTreeNode(String path, TreeNode parent) {
file = new File(path);
children = new Hashtable();
}

public TreeNode getChildAt(int childIndex) {
String[] names = file.list();
String name = names[childIndex];
return getChild(name);
}

public int getChildCount() {
if (file.isDirectory()) {
return file.list().length;
}
return 0;
}

public TreeNode getParent() {
return parent;
}

public int getIndex(TreeNode node) {
String name = ((FileTreeNode)node).file.getName();
String[] names = file.list();
for (int i=0; i<names.length; i++)
if (names.equals(name))
return i;
return -1;
}

public boolean getAllowsChildren() {
return file.isDirectory();
}

public boolean isLeaf() {
return !file.isDirectory();
}

public Enumeration children() {
return new ChildrenEnumeration();
}

public String toString() {
return file.getName();
}

private FileTreeNode getChild(String name) {
if (children.containsKey(name))
return (FileTreeNode)children.get(name);
try {
String path = file.getCanonicalPath() + "/" + name;
FileTreeNode child = new FileTreeNode(path, this);
children.put(name, child);
return child;
} catch (IOException e) {
return null;
}
}

private class ChildrenEnumeration implements Enumeration {
String[] names;
int index;

public ChildrenEnumeration() {
names = file.list();
}

public boolean hasMoreElements() {
return index < names.length - 1;
}

public Object nextElement() {
String name = names[index];
index++;
return getChild(name);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top