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;
}
}
}
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;
}
}
}