[jdom-interest] jdom tree conversion to a JTree

Richard Cook rpc at prismtechnologies.com
Thu May 3 06:00:45 PDT 2001

Depends on what you need, but say you want to display the elements could you
not just implement TreeModel:

class JDOMTreeModel implements TreeModel {
   private Element root;
   public JDOMTreeModel(Element root) {
      this.root = root;
   public Object getChild(Object parent, int index) {
      return ((Element) parent).getChildren().get(index);
   public int getChildCount(Object parent) {
      return ((Element) parent).getChildren().size();
   public int getIndexOfChild(Object parent, Object child) {
      return ((Element) parent).getChildren().indexOf(child);
   public Object getRoot() {
      return root;
   public boolean isLeaf(Object node) {
      return false; // whatever
   // etc

then TreeModel mdl = new JDOMTreeModel(doc.getRootElement());
     JTree tree = new JTree(mdl)

saves serialising the JDOM tree or having to walk it. You can change the
cell renderer to display the element name rather than the toString(), or
wrap the "Element" in getChild() if necessary [ return new MyNode(((Element)
parent).getChildren().get(index)) ]. If you need attrs there'd be a bit more

-----Original Message-----
From: jdom-interest-admin at jdom.org [mailto:jdom-interest-admin at jdom.org]On
Behalf Of Kiss Gábor
Sent: Thursday, May 03, 2001 8:11 AM
To: jdom-interest at jdom.org
Subject: [jdom-interest] jdom tree conversion to a JTree

How can I convert a jdom tree to a JTree?


Kiss Gábor

More information about the jdom-interest mailing list