DOM extension of JTree

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




}
 
T

The Abrasive Sponge

Hey Arun,

I thought I'd give myself the challenge, anyways here is the code (30
minutes, heavy copying of pre-existing stuff). I hope you can find this
clear and helpful. I usually don't do this often for newsgroup people,
but this kind of thing would be helpful for my toolkit. There are two
source files here, and each item has it's place, I think you may find
this clearer than what you had. Buy me a beer sometime if you like it.
You will have to do some tweaking to get the attribute in there, but
thar you go.


//Source JDOMTreeModel

package com.evolutionnext.jdom;
import org.jdom.*;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.util.Vector;

/**
*
* @author Administrator
*/
public class JDOMTreeModel implements TreeModel{

private boolean showAncestors;
private Vector treeModelListeners = new Vector();
private Element rootElement;


/** Creates a new instance of JDOMTreeModel */
public JDOMTreeModel(Document document) {
this.rootElement = document.getRootElement();
}

/**
* Used to toggle between show ancestors/show descendant and
* to change the root of the tree.
*/
public void showAncestor(boolean b, Object newRoot) {
showAncestors = b;
Element oldElement = rootElement;
if (newRoot != null) {
rootElement = (Element)newRoot;
}
fireTreeStructureChanged(oldElement);
}


//////////////// Fire events
//////////////////////////////////////////////

/**
* The only event raised by this model is TreeStructureChanged with the
* root as path, i.e. the whole tree has changed.
*/
protected void fireTreeStructureChanged(Element oldRoot) {
int len = treeModelListeners.size();
TreeModelEvent e = new TreeModelEvent(this,
new Object[] {oldRoot});
for (int i = 0; i < len; i++) {
((TreeModelListener)treeModelListeners.elementAt(i)).
treeStructureChanged(e);
}
}



/**
* Adds a listener for the TreeModelEvent posted after the tree
changes.
*/
public void addTreeModelListener(TreeModelListener l) {
treeModelListeners.addElement(l);
}

/**
* Returns the child of parent at index index in the parent's child
array.
*/
public Object getChild(Object parent, int index) {
Element parentElement = (Element) parent;
if (showAncestors) {

return parentElement.getParentElement();
}
return parentElement.getChildren().get(index);
}

/**
* Returns the number of children of parent.
*/
public int getChildCount(Object parent) {
Element parentElement = (Element)parent;
if (showAncestors) {
int count = 0;
if (parentElement.getParentElement() != null) {
count++;
}
return count;
}
if (parentElement.getChildren() == null) return 0;
return parentElement.getChildren().size();
}

/**
* Returns the index of child in parent.
*/
public int getIndexOfChild(Object parent, Object child) {
Element element = (Element)parent;
if (showAncestors) {
int count = 0;
if (element.getParentElement() != null) {
count++;
if (element == element.getParentElement()) return 0;
return -1;
}
}
return element.getChildren().indexOf(child);
}

/**
* Returns the root of the tree.
*/
public Object getRoot() {
return rootElement;
}

/**
* Returns true if node is a leaf.
*/
public boolean isLeaf(Object node) {
Element element = (Element)node;
if (showAncestors) {
return element.getParentElement() == null;
}
return element.getChildren() == null;
}

/**
* Removes a listener previously added with addTreeModelListener().
*/
public void removeTreeModelListener(TreeModelListener l) {
treeModelListeners.removeElement(l);
}

/**
* Messaged when the user has altered the value for the item
* identified by path to newValue. Not used by this model.
*/
public void valueForPathChanged(TreePath path, Object newValue) {
System.out.println("*** valueForPathChanged : "
+ path + " --> " + newValue);
}

}



//Source: JDOM Tree
package com.evolutionnext.jdom;
import org.jdom.*;
import javax.swing.*;

/**
*
* @author Administrator
*/
public class JDOMTreeExample extends JFrame {

/** Creates a new instance of JDOMTree */
public JDOMTreeExample() {
super("tree");

Document document = new Document();
Element root = new Element("root");
Element child1 = new Element("child1");
Element child2 = new Element("child2");
Element grandchild1 = new Element("grandchild1");

Attribute att = new Attribute("amIcool", "Oh, yeah");
child1.setAttribute(att);
child1.addContent(grandchild1);
root.addContent(child1);
root.addContent(child2);
document.setRootElement(root);

JDOMTreeModel treeModel = new JDOMTreeModel(document);
JTree tree = new JTree(treeModel);

getContentPane().add(new JScrollPane(tree));

setSize(500,500);
}

public static void main(String[] args) {
JDOMTreeExample example = new JDOMTreeExample();
example.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
example.setVisible(true);
}

}
 

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

JTree model update problem 3
change icon in jtree 0
JTable within JTree 1
need to insert a parent in a jtree 5
Extra Row in JTree 6
Programmatically removing nodes from a JTree 2
Jtree renderers 0
JTree oddity... 5

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top