Your suggestion, is it basically saying to use a DOM parser to build a
number of org...Element objects in a tree style, then build the JTree
from that. I'm a little confused by your answer.
What I said was to parse the XML document to a DOM and then, not build a
tree from that, but write a TreeModel implementation based on it. For
example:
public class DOMTreeModel implements TreeModel
{
private org.w3c.dom.Document document;
public DOMTreeModel(org.w3c.dom.Document doc)
{
this.document = doc;
}
// ... implement TreeModel methods here ...
}
The type for a tree node in TreeModel is Object, so you can return
anything. The best plan is to use Node instances from the DOM as tree
nodes. Each of the TreeModel methods will call methods in the document
or on the Node object (which you'll need to cast after it's passed in)
and return the appropriate information.
If (and only if) you intend to change the document while it's displaying
in a tree, then you also need to use the DOM Events specification, and
convert any DocumentEvent from the DOM level into a TreeModelEvent. I
suggested Xerces because Java 1.4's JAXP implementation doesn't provide
events. It appears, from a look at the API documentation, that 1.5
does. If you're using 1.5, you may not need to bother with Xerces.
Also, if you don't intend to change the document as you're displaying
it, then you don't need to bother with any of this.
As for a TreeCellRender, whoosh, thats gone straight over my head. I'm
not looking to manipulate the tree once its been built. It's just there
for reference for the user, so if thats what it's used for then I guess
it doesn't really apply for me.
TreeCellRenderer is necessary, but it's not hard. Here's a simple
example:
public class DOMTreeCellRenderer extends DefaultTreeCellRenderer
{
public Component getTreeCellRendererComponent(
JTree tree, Object value, boolean sel, boolean expanded,
boolean leaf, int row, boolean hasFocus)
{
org.w3c.dom.Node node = (org.w3c.dom.Node) value;
String text = node.getNodeName();
return super.getTreeCellRendererComponent(
tree, text, sel, expanded, leaf, row, hasFocus);
}
}
Also, I tried installing Xerces DOM parser last night, but I couldn't
manage to cos I'm a bit thick or something. Do you know of a
step-by-step guide to installing any DOM parser?
Xerces can be used just like any other Java library. Once you install
it, you can parse a document by creating an instance of
org.apache.xerces.parsers.DOMParser, and calling its parse method.
Again, though, if you're using 1.5 or don't need to keep the display up
to date with changes to the document, then you can skip this step.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation