JTree <--> JDom

hi, i need to make an application that load an xml doc into a jtree. Then the user should be able to edit node content with a jtextarea and finally save changes. To load xml document to jtree i did:

private DefaultMutableTreeNode createTreeNode(Element node){

DefaultMutableTreeNode treeNode =new DefaultMutableTreeNode(node.getName());

List children = node.getChildren();

Element child;

for (int i = 0; i < children.size(); i++){

child = (Element) children.get(i);

if (childinstanceof Element){

treeNode.add(createTreeNode(child));

}

}

return treeNode;

}

public DefaultTreeModel getTreeModel(){

DefaultMutableTreeNode root = createTreeNode(document.getRootElement());

DefaultTreeModel treeModel =new DefaultTreeModel(root);

return treeModel;

}

then i set that treeModel to tree.setModel().

The problem is the following: when i click on a jtree node i wish to show into jtextarea the content of the node represented by the jtree-node clicked. In the treeEvent handler i don't know how to get the right node in the jdom document that correspond to the jtree node clicked.

I thought to set an id to my jdom document nodes, then on treeEvent, check which one is selected, get enumeration by depthFirst() method, and so on....

Is there any clever way to do that?

Thanks

[1958 byte] By [Hanz0a] at [2007-10-2 6:44:12]
# 1

A JDOM document forms a natural tree structure and I have a TreeModel adapter for this. Creating the adapter to just allow the display of the data is fairly straightforwards but it will require a bit more effort to make an editable adapter. At this time I cannot relase my code.

If you are going to use DefaultMutableTreeNode then you can build the path of JDOM elements from a node using the DefaultMutableTreeNode.getUserObjectPath() method and then you can cast the items to JDOM elements.

sabre150a at 2007-7-16 13:52:44 > top of Java-index,Desktop,Core GUI APIs...
# 2
HANZ0 can u tell me how u solved this problem of editing the xml content with java swing ...i am in need of same solution for my code ....hope u would replythanks a lot
avidcodera at 2007-7-16 13:52:44 > top of Java-index,Desktop,Core GUI APIs...