File Tree using JTree
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Susan_Hetherington
Posted On:   Friday, April 25, 2003 04:25 AM

I have created a tree that show the hierarchy ina directory on my c drive. Via my GUI the user can add files to this directory and so I need them to show up on the tree and be accessible as well. I have the new node adding dynamically to the tree but it appears as a folder not a document so I am unable to view the contents of the document. Can anybody help??? Heres my tree code: import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.tree.*; import javax.swing.event.*; /** * A TreeNode that contains a File object */ class FileTreeNode extends DefaultMutableTreeNode {    More>>

I have created a tree that show the hierarchy ina directory on my c drive. Via my GUI the user can add files to this directory and so I need them to show up on the tree and be accessible as well. I have the new node adding dynamically to the tree but it appears as a folder not a document so I am unable to view the contents of the document. Can anybody help???




Heres my tree code:





			
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;

/**
* A TreeNode that contains a File object
*/
class FileTreeNode extends DefaultMutableTreeNode {
boolean hasbeenread;

public FileTreeNode(File file) {
super(file);
hasbeenread = false;
}

public boolean readTree() {
return readTree(false);
}

public boolean readTree(boolean b) {
if(hasbeenread)
return false;

String list[] = getFileObject().list();
if(list != null) {
for(int i = 0; i
FileTreeNode subnode = new FileTreeNode(new File(getFileObject(),
list[i]));
add(subnode);
if(b)
subnode.readTree(b);
}
}
hasbeenread=true;
return true;
}

public File getFileObject() {
return (File)getUserObject();
}

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

public boolean isLeaf() {
return ((File)userObject).isFile();
}
}


/**
* Listens for expansion events
*/
class HierarchyBrowserUpdater implements TreeExpansionListener {
FileTreeNode root;
JTree jtree;
DefaultTreeModel treemodel;
JPanel frame;

public HierarchyBrowserUpdater(JTree tree, FileTreeNode node, DefaultTreeModel tmodel, JPanel theFrame)
{
root = node;
jtree = tree;
treemodel = tmodel;
frame = theFrame;
}

public void treeExpanded(TreeExpansionEvent event) {

TreePath path = event.getPath();

System.out.print("Received expansion event on ");
System.out.println(path);

FileTreeNode node = (FileTreeNode)path.getLastPathComponent();
System.out.println("Node level: " + node.getLevel());
System.out.println("Children " + node.getChildCount());
System.out.println("Reading subtree " + node.toString());

frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
frame.setEnabled(false);

if(node.readTree()) {
System.out.println("Calling nodesWereInserted");
int childrenIdx[] = new int[node.getChildCount()];
int i=0;
for(Enumeration e = node.children(); e.hasMoreElements();) {
Object obj = e.nextElement();
childrenIdx[i] = node.getIndex((TreeNode)obj);
i++;
}
treemodel.nodesWereInserted(node, childrenIdx);
}
frame.setEnabled(true);
frame.setCursor(Cursor.getDefaultCursor());
}

public void treeCollapsed(TreeExpansionEvent event)
{
}
}

class MyTreeModelListener implements TreeModelListener {
public void treeNodesChanged(TreeModelEvent e) {
FileTreeNode node;
node = (FileTreeNode)
(e.getTreePath().getLastPathComponent());

/*
* If the event lists children, then the changed
* node is the child of the node we've already
* gotten. Otherwise, the changed node and the
* specified node are the same.
*/
try {
int index = e.getChildIndices()[0];
node = (FileTreeNode)
(node.getChildAt(index));
} catch (NullPointerException exc) {}

System.out.println("The user has finished editing the node.");
System.out.println("New value: " + node.getUserObject());
}
public void treeNodesInserted(TreeModelEvent e) {
}
public void treeNodesRemoved(TreeModelEvent e) {
}
public void treeStructureChanged(TreeModelEvent e) {
}
}

/**
* Shows a tree structure (of a directory)
*/
class MyTree extends JPanel
{
JTree theTree;
DefaultTreeModel treemodel;
FileTreeNode root;

public MyTree(String dir) {
setLayout(new BorderLayout());
addNotify();

root = (FileTreeNode)buildTree(dir);
treemodel = new DefaultTreeModel(root);
theTree = new JTree(treemodel);
theTree.setDoubleBuffered(true);

HierarchyBrowserUpdater upd = new HierarchyBrowserUpdater(theTree, root, treemodel, this);
theTree.addTreeExpansionListener(upd);
treemodel.addTreeModelListener(new MyTreeModelListener());
//theTree.addMouseListener(this);

JScrollPane scrollpane = new JScrollPane(theTree);
scrollpane.setPreferredSize(new Dimension(200, 585));
scrollpane.setDoubleBuffered(true);

add(scrollpane);
}

public void redraw()
{
((DefaultTreeModel)theTree.getModel()).reload(root);
}

public TreeNode buildTree(String dir) {
FileTreeNode root = new FileTreeNode(new File(dir));
root.readTree(false);
return (TreeNode)root;
}

/** Add child to the currently selected node. */
public FileTreeNode addObject(File child) {
FileTreeNode parentNode = null;
TreePath parentPath = theTree.getSelectionPath();

if (parentPath == null) {
parentNode = root;
} else {
parentNode = (FileTreeNode)
(parentPath.getLastPathComponent());
}

return addObject(parentNode, child, true);
}

public FileTreeNode addObject(FileTreeNode parent,
File child) {
return addObject(parent, child, false);
}

public FileTreeNode addObject(FileTreeNode parent,
File child,
boolean shouldBeVisible) {
FileTreeNode childNode =
new FileTreeNode(child);

if (parent == null) {
parent = root;
}
childNode.isLeaf();
treemodel.insertNodeInto(childNode, parent,
parent.getChildCount());

// Make sure the user can see the lovely new node.
if (shouldBeVisible) {
theTree.scrollPathToVisible(new TreePath(childNode.getPath()));
}
return childNode;
}
}





Thanks a million!!!    <<Less
About | Sitemap | Contact