October 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Displaying XML in a Swing JTree

  • March 3, 2008
  • By Rob Lybarger
  • Send Email »
  • More Articles »

Overview

It seems obvious enough: You have an XML document or fragment. XML is hierarchical. A Swing JTree displays hierarchical data. How do you make a JTree display your XML fragment?

If you understand that Swing's architecture uses MVC, you probably know you need a "model" that your JTree instance can be instructed to use. However, the only real concrete model class in the standard Swing API is the DefaultTableModel class. This class provides objects to the tree that implement the TreeNode interface. If you have started down this path, subclassing and customizing the standard behavior of the DefaultTableModel and working with your own DefaultTreeNode objects just to display XML will quickly give you a headache.

Fortunately, the better answer is to bypass all the API-provided convenience classes and instead write your own custom implementation of the javax.swing.tree.TreeModel interface. This article will show you what you need to do.

About this Article

I will be staying completely within the standard API, so no third-party XML libraries will be needed. If you need to get more familliar with the XML classes in the standard API, you might want to read one of my earlier articles "Working with XML and Java" or consult your favorite search engine. I also tend to use Java 5 syntax (generics and enhanced-for). The reader is assumed to be somewhat familiar with Swing.

The TreeModel Interface

This interface defines the following methods. I have borrowed the descriptions directly from the Java API documentation.

  • void addTreeModelListener(TreeModelListener l): Adds a listener for the TreeModelEvent posted after the tree changes.
  • void removeTreeModelListener(TreeModelListener l): Removes a listener previously added with addTreeModelListener.
  • Object getRoot() Returns the root of the tree.
  • int getChildCount(Object parent): Returns the number of children of parent.
  • Object getChild(Object parent, int index): Returns the child of parent at index index in the parent's child array.
  • boolean isLeaf(Object node): Returns true if node is a leaf.
  • int getIndexOfChild(Object parent, Object child): Returns the index of child in parent.
  • void valueForPathChanged(TreePath path, Object newValue): Messaged when the user has altered the value for the item identified by path to newValue.

I have listed the methods roughly in order of usage. When a JTree is given a TreeModel implementation to use, it registers itself as a TreeModelListener. (A good model implementation will alert all of its listeners if the structure of the tree changes, or if a node value changes. This lets the tree know it needs to redraw itself.) The root node is consulted first, and then the children for each node are obtained to build up the display. The icon in the tree that appears to each entry is determined from the result of whether that entry is a leaf or not. (Generally, if a node returns '0' for getChildCount, it should return 'true' for isLeaf... but, this is not set in stone.) Finally, if the values in the model are mutable (they can be edited) and the tree is editable, the tree will communicate with the edited value with the model by way of the valueForPathChanged method. (This example won't use an editable tree, so you won't implement this last method.)

An important point to note is that, being an interface, the TreeModel class doesn't concern itself with exactly where the data comes from or how it is stored. Because you will be dealing with an XML document, however, your implementation will include an instance field for an org.w3c.dom.Document class, and a corresponding getter/setter method pair. The TreeModel interface methods will simply work off of the Document object directly:

protected Document document;

public Document getDocument() {
   return document;
}

public void setDocument(Document doc) {
   this.document = doc;
   TreeModelEvent evt = new TreeModelEvent(this,
      new TreePath(getRoot()));
   for (TreeModelListener listener : listeners) {
      listener.treeStructureChanged(evt);
   }
}

Any time you alter the data model—such as when you replace the source XML document completely in the setDocument method—you need to alert all the listeners of this fact. This is what the extra code in setDocument takes care of. (The definition of the "listeners" variable will be introduced shortly. Bear with me.)

Before you start writing the TreeModel method implementations, you should carefully note that the model returns back objects of type java.lang.Object to the tree (for the root node and all children underneath it). The tree, by way of its default TreeCellRenderer, neither knows nor cares exactly what class these objects truly are. To know what label to draw in the tree for any given node, the toString method is called. (A special-purpose, highly customized tree might use a different tree cell renderer, which might be written to use something other than 'toString' to generate the node labels ... but that is beyond the scope of this article.) With this being the case, the standard org.w3c.dom.Node class doesn't provide a terribly useful return value for the toString method... at least not that useful to the casual end-user. To address this, you will wrap the Element objects in a custom class that provides a more intelligent response to the toString method: It will return the name of the Element using the getNodeName method. Look at this wrapper class first:

public class XMLTreeNode {
   Element element;
   public XMLTreeNode(Element element) {
      this.element = element;
   }
   public Element getElement() {
      return element;
   }
  public String toString() {
      return element.getNodeName();
   }
}




Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel