Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Roll Your Own Swing-based XML Editor (Part I)-3 : Page 3


Working with the JTree Component
In nature, a tree has a common trunk from which branches extend. From those branches more branches extend. Every branch is related to every other branch in some way because they all have a common source, the trunk. This hierarchical relationship is not unique to trees; human genealogies follow much the same pattern. From a common set of parents, extend one or more children, each of those children potentially have one or more children, and the pattern continues ad infinitum.

In terms of data storage, a tree is a way of storing data that is organized in a manner similar to a genealogical tree. Each branch of the tree is called a node. The common parent node to all child nodes is called the root node. Every node that has child nodes is called a parent node (even if it, in turn, is a child of another node). A JTree component is simply a visual representation of a tree data structure.

Figure 1 shows a screen shot of a JTree component.

Almost every XML editor includes a visual tree structure like this one that allows you to edit and navigate through the elements contained in the XML document. We will construct one of these in the next section, but first we need to learn a little more about the JTree component.

A node stores data at a particular position in the tree. In addition to containing data, it knows about its parent node and any child nodes for which it is a parent. The javax.swing.tree package defines a handful of interfaces that provide a common way of building and manipulating a tree structure.

  • TreeNode—declares methods used to access information about a tree node
  • MutableTreeNode—declares methods used on a mutable tree (one that can add and remove child nodes)
  • TreeModel—declares methods used to create and manage the data model associated with a tree.

Next, we'll create a class that extends JTree and provides methods that allow an XML document to be parsed and displayed as visual nodes in a JTree component.

Creating the XTree Component
The XTree class consists of one constructor and three methods. Our simple implementation of the XTree component merely builds an Xtree; it does not provide for any manipulation of tree nodes after the tree has initially been built. As such, every method within the XTree class is called from the constructor.

Here's a look at XTree API:

Field Summary

  • private DefaultMutableTreeNode treeNode—This member stores the TreeNode object used to create the model for the JTree. The DefaultMutableTreeNode class is defined in the javax.swing.tree package and provides a default implementation of the MutableTreeNode interface.
  • private DocumentBuilderFactory dbf
  • private DocumentBuilder db
  • private Document doc—These three members are a part of the JAXP API and are used to parse the XML text into a DOM object (of type Document).

Constructor Summary

  • public XTree( String text )—This single constructor builds an XTree object using the XML text passed in through the constructor. After initializing some of the basic display properties that relate to the JTree superclass and the DOM parsing objects, the constructor builds a TreeModel object to create the actual visual tree. To do this, a root node is created by passing the DOM object into the createTreeNode() method which returns an object of type DefaultMutableTreeNode. This object is then used to create the tree's TreeModel via the setModel() method inherited from the JTree class.

Method Summary

  • private DefaultMutableTreeNode createTreeNode( Node root )—This method takes a DOM Node and recurses through the children until each one is added to a DefaultMutableTreeNode. This is a recursive method, calling itself for each child node found beneath the root node. The JTree then uses the DefaultMutableTreeNode object as a tree model.
  • private String getNodeType( Node node ) —This method is used by createTreeNode() to associate a string of text with a particular type of node.
  • private Node parseXml()—This method performs the actual parsing of the XML text string. It returns an object of type Node that can then be passed into the createTreeNode() method.

There you have it: a class that extends JTree, adding XML text parsing capabilities. In the next two articles we will build additional components and extend upon this component to create a simple, platform-independent XML editor.

Below, you will find links to two java source files, a sample XML file, and the xerces.jar with the JAXP classes and Xerces parser. To experiment with the XTree class, follow the following steps:

  • download the four files;
  • make sure the xerces.jar file is in your classpath;
  • compile the two java source files;
  • run the XTreeTester class and pass the sample XML file as an argument like this: java XTreeTester sample.xml

    XTree.java—The XTree component class.
    XTreeTester.java—This file contains a main() method and is used to test the XTree class by parsing an XML file.
    Sample.xml—A sample XML file to pass into the XTreeTester class.
    xerces.jar—This jar file contains the Xerces parser and the JAXP classes required by the XTree class. It must be in your classpath for the application to successfully compile and execute.

DevX Java Pro Kyle Gabhart is a consultant for Objective Solutions, a high-end engineering services company focusing on the medical, semiconductor, and telecommunications domains. He is an Associate Member of the Worldwide Institute of Software Architects (WWISA), an organization dedicated to promoting and enriching software architecture as a profession. Kyle is a co-author of "Professional Java and XML".
Comment and Contribute






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



Thanks for your registration, follow us on our social networks to keep up-to-date