Swing JTree, XML, display and update

hi, calling xml processing experts.

I am developing a GUI with a JTree component t display some xml file content in hierarchical order, and display the attributes in a JTable when user selects a leaf node. User could also modify the attributes in the JTable. So far, everything accomplished, and works fine.

However, I wish some one could offer me some expert opinion to optimize some of the implementations.

1) i am using a org.jdom.Document as the data in the TreeModel, however, the when the JTable query iterates through the Document, it will display the string returned from the Element.toString(). (that is my understanding). The toString() method returns something like 'Element:Nodename' kind of thing. Thus, i have to develop a ElementWrapper which takes a org.jdom.Element as constructor argument, and returns the string which i want.

So, in the treeModel, i have to add some code, to wrap/unwrap the element, and it is maybe error prone. i hope somebody could provide me some better idea/hints?

I have attempted several ways:

* rewrite the Element in the org.jdom package, (seems ...ew... a bit awkward)

* try to create a class which extends org.jdom.Element, however, the Element works in a hierarchy, and the interface like getChild() always return a Element instance, and it does not allow down cast.

2) i want to add in some filter function on the JTree display, wich means i only want to display Elements with certain attributes. i need some Element invisible in the JTree display, however, they still needs to be in the Document tree, since i need to write them back.

How could i be able to implement it? also, i could do the filtering in the treeModel, however, i now find some hot discussion on XPath related stuff, how am i going to making use of that?

Lots of thanks in advance.

[1868 byte] By [sqra] at [2007-10-2 18:42:24]
# 1

I can help you with your question 1.

The JavaDoc for JTree says:

To use JTree to display compound nodes (for example, nodes containing both a graphic icon and text), subclass TreeCellRenderer and use setCellRenderer(javax.swing.tree.TreeCellRenderer) to tell the tree to use it.

The TreeCellRenderer interface has only one required method "getTreeCellRendererComponent" that returns a Component. In your case, you can return a JLabel with whatever text in it you want. With that method you are passed an Object that has the value of this tree node (in your case, an Element object) and you can cast it to an Element and get whatever information you wish.

On your second questions, if you have XML like

<a>

<b att='1'>

<c att='2'/>

</b>

</a>

and only want to display elements a and c, I don't know how you could represent this. If, however, your data looks more like:

<root>

<node value="a">

...

</node>

<node value="b">

...

</node>

<node value="a">

...

</node>

</root>

and what you want is to display the first and third elements, that could be done by rebuilding the treeModel.

Dave Patterson

d.pattersona at 2007-7-13 20:04:38 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

hi, Patterson, thank you very much for the advice.

Q1. i will look into your suggestions, and see how it works

Q2. i mean to hide some leaf nodes in the JTree, and they are at the same level of depth. could you provide some details? maybe some code snippet?

do u mean i should rewrite some of the treeModel interface, to let them return null or something?

thanks

sqra at 2007-7-13 20:04:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

I've not personally done the TreeRenderer kind of code. I've done TableRenderer which is similar.

I'm not sure what happens if the renderer returns a null. If you return a JLabel that has an empty string as its text, it will still take up vertical space. I am not aware of how you can apply a filter to a JTree's data model. If there is no filter available, you will have to create a new TreeModel that contains only the contents you want to show. I did not mean that you need to rewrite any of the TreeModel's interfaces. Just supply JTree with a new model.

Dave Patterson

d.pattersona at 2007-7-13 20:04:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
Hi,The code to hide/show nodes of a jtree is available at http://forum.java.sun.com/thread.jspaorumID=57&threadID=563514cheers,vidyut http://www.bonanzasoft.com
vidyuta at 2007-7-13 20:04:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
hi, I find the link is broken.
sqra at 2007-7-13 20:04:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
No, just badly posted. http://forum.java.sun.com/thread.jspa?forumID=57&threadID=563514
DrClapa at 2007-7-13 20:04:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...