Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Capture hierarchical structures with Swing's JTree

Find out how Java's new standard tree component helps you to handle one of the most common programmatic functions

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 5

All Swing data-representation components are implemented with this design pattern in mind, so all Swing applications will inherently benefit from this immense design flexibility.

JTree component architecture
The following figure illustrates the Model and UI (View-Controller) portions of the JTree component.

Swing's JTree component architecture

  • The UI part of JTree, the portion that manages the rendering of the tree data, is defined by the TreeUI interface.

  • The tree data, which is hierarchical by nature, is held by a TreeModel object. As you'll see later, the data can be any arbitrary hierarchy of information; it need not even all be held in memory at once, it can be simply fetched as needed.

  • The selection model is handled by TreeSelectionModel; this allows Swing to manage different selection strategies (continuous selection, discontinuous selection, for example)


We'll now take a more detailed look at the classes that are involved in Swing's tree component:

Swing tree package

Most of JTree's model entities are part of the com.sun.java.swing.tree package. Below is a simple class diagram that describes the main entities and the relationships between them. (Note that there are more classes in the package, but I omitted those that aren't directly related to the model part of JTree.)

Main entities of the Swing tree package

In keeping with good design practice, the tree package is divided into abstraction and implementation layers. In fact, the tree package provides several degrees of abstraction. At the highest level are the TreeModel, TreeSelectionModel, and TreePath interfaces and classes, which simply describe a data/selection model of Objects. Practical implementations of these interfaces are provided by DefaultTreeModel and DefaultTreeSelectionModel, which describe a tree consisting of TreeNode and MutableTreeNode objects. Finally, a practical implementation of these node interfaces is provided by DefaultMutableTreeNode. These different levels of abstraction are described in detail below.

The tree package interfaces

Interfaces denote the contract that a given component must fulfill in order to participate in a particular collaboration. The tree package defines one main data model contract -- the TreeModel interface, and two node interfaces, TreeNode and MutableTreeNode -- that you use when working with the default tree model implementation. Data-related communications are performed with TreeModelEvent and TreeModelListener.

Let's examine these contracts (interfaces) in more detail.

The TreeModel contract
The TreeModel interface defines the contract that a general tree-like hierarchical collection for a hierarchical data must fulfill. The interface should be implemented by any class that holds hierarchical data (the File system, for example) and wishes to use the JTree component as its GUI. In order to keep the contract as general as possible, all of the TreeModel methods accept and return an Object class, instead of a more constrained type, which allows you to implement the interface for constructing your own hierarchical data structures of arbitrary datatypes.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • Download the complete source as a gzipped tar file http://www.javaworld.com/jw-10-1998/jtree/jw-10-jtree.tar.gz
  • Download the complete source as a zip file http://www.javaworld.com/jw-10-1998/jtree/jw-10-jtree.zip
  • Tree package API http://java.sun.com/products/jfc/swingdoc-api/com/sun/java/swing/tree/package-summary.html
  • Tree listeners and event API http://java.sun.com/products/jfc/swingdoc-api/com/sun/java/swing/event/package-summary.html
  • Stop by the Swing applet page to find out how to run Swing applets on Netscape and Internet Explorer http://java.sun.com/products/jfc/swingdoc-current/applets.html
  • How-To Java columnist Todd Sundsted provides a great backgrounder on the Model-UI pattern in "MVC meets Swing" (JavaWorld, April 1998) http://www.javaworld.com/jw-04-1998/jw-04-howto.html
  • Add 1.1 support to Netscape Communicator4.0x with these step-by-step instructions for applying the 1.1 support patch http://developer.netscape.com/software/jdk/download.html
  • Download the JDK 1.1 Preview Release for Communicator 4.05 http://developer.netscape.com/software/jdk/download.html
  • Find out more about patterns at the design pattern home page http://hillside.net/patterns/patterns.html
  • If you're interested in analysis patterns, I recommend you pick up a copy of Martin Fowler's book Analysis PatternsReusable Object Models http://hillside.net/patterns/books/#Analysis
  • Check out Design PatternsElements of Reusable Object-Oriented Software for a thorough discussion designing trees nodes http://hillside.net/patterns/books/#Gamma
  • Read about UML at http://www.rational.com/uml