A
Arun
Hi,
I've created the following extension of a JTree.
However when I try to create an instance of it, it doesnt work.
Instead the tree just contains the default "colors, sports, foods"
nodes.
The class takes as an input a DOM and should process this DOM into the
JTree. I kinda know some parts work, like addChildNodes class, because
i did a system output and it outputted all the nodes in the xml file i
inputed (which was converted to a dom first).
Could someone please help me?
The dom processing is done with JDOM.
I create an instance of the Tree like so:
JTree tree = new XMLTree(xmlDOM);
This is the class:
******************************************************************
import java.util.List;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import org.jdom.Document;
import org.jdom.Element;
public class XMLTree extends JTree{
public XMLTree(Document doc)
{
makeRootNode(doc);
}
private static DefaultMutableTreeNode makeRootNode(Document xmlDom)
{
// Recursively descends the tree and copies corresponding
// dom nodes.
try{
Element rootElement = xmlDom.getRootElement();
DefaultMutableTreeNode rootNode = buildTree(rootElement);
return(rootNode);
}
catch(Exception e) {
// Returns error message if building of tree is not successful
String errorMessage = "Error making root node: " + e;
System.err.println(errorMessage);
e.printStackTrace();
return(new DefaultMutableTreeNode(errorMessage));
}
}
private static DefaultMutableTreeNode buildTree(Element
rootElement)
{
// Makes a JTree node for the root, then makes JTree
// nodes for each child and adds them to the root node.
DefaultMutableTreeNode rootNode = new
DefaultMutableTreeNode(rootElement.getName());
addChildNodes(rootNode,rootElement);
return(rootNode);
}
private static void addChildNodes(DefaultMutableTreeNode
parentNode, Element parentElement)
{
// Creates list of all children of current parent element
List allChildren = parentElement.getChildren();
// Checks to see the element has any children
if (allChildren.size() != 0)
{
for( int x = 0; x < allChildren.size(); x++)
{
// Creates a new element from current element in list
Element childElement = (Element)allChildren.get(x);
// Converts this element into a node
DefaultMutableTreeNode childNode = new
DefaultMutableTreeNode(childElement.getName());
// Adds this node to the current parent node
parentNode.add(childNode);
// Checks to see if this child node has a child node of
itself.
// If so, then adds child node to that. Recursive procedure.
addChildNodes(childNode, childElement);
}
}
}
}
I've created the following extension of a JTree.
However when I try to create an instance of it, it doesnt work.
Instead the tree just contains the default "colors, sports, foods"
nodes.
The class takes as an input a DOM and should process this DOM into the
JTree. I kinda know some parts work, like addChildNodes class, because
i did a system output and it outputted all the nodes in the xml file i
inputed (which was converted to a dom first).
Could someone please help me?
The dom processing is done with JDOM.
I create an instance of the Tree like so:
JTree tree = new XMLTree(xmlDOM);
This is the class:
******************************************************************
import java.util.List;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import org.jdom.Document;
import org.jdom.Element;
public class XMLTree extends JTree{
public XMLTree(Document doc)
{
makeRootNode(doc);
}
private static DefaultMutableTreeNode makeRootNode(Document xmlDom)
{
// Recursively descends the tree and copies corresponding
// dom nodes.
try{
Element rootElement = xmlDom.getRootElement();
DefaultMutableTreeNode rootNode = buildTree(rootElement);
return(rootNode);
}
catch(Exception e) {
// Returns error message if building of tree is not successful
String errorMessage = "Error making root node: " + e;
System.err.println(errorMessage);
e.printStackTrace();
return(new DefaultMutableTreeNode(errorMessage));
}
}
private static DefaultMutableTreeNode buildTree(Element
rootElement)
{
// Makes a JTree node for the root, then makes JTree
// nodes for each child and adds them to the root node.
DefaultMutableTreeNode rootNode = new
DefaultMutableTreeNode(rootElement.getName());
addChildNodes(rootNode,rootElement);
return(rootNode);
}
private static void addChildNodes(DefaultMutableTreeNode
parentNode, Element parentElement)
{
// Creates list of all children of current parent element
List allChildren = parentElement.getChildren();
// Checks to see the element has any children
if (allChildren.size() != 0)
{
for( int x = 0; x < allChildren.size(); x++)
{
// Creates a new element from current element in list
Element childElement = (Element)allChildren.get(x);
// Converts this element into a node
DefaultMutableTreeNode childNode = new
DefaultMutableTreeNode(childElement.getName());
// Adds this node to the current parent node
parentNode.add(childNode);
// Checks to see if this child node has a child node of
itself.
// If so, then adds child node to that. Recursive procedure.
addChildNodes(childNode, childElement);
}
}
}
}