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
By Tomer Meshorer, JavaWorld.com, 10/01/98
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Trees allow us to represent hierarchies of data in a convenient, accessible form. They are important because hierarchies of
data are extremely common -- from the contents of your hard drive to the interconnections of a global public-key certification
framework. Swing's
JTree component enables developers to easily capture (represent in a data structure) and display hierarchies. Before Swing, there
was no standard JDK tree component; furthermore, few of the third-party options were as well designed as this: Like all Swing
components,
JTree is built around the Model-UI pattern -- a design in which each component is divided into a model part that holds the data
and UI part that renders it.
Rather than cram both aspects of the Model-UI pattern into one huge article, I've decided to split up the concept into two,
more easily digestible articles. This month, I'll demonstrate how to use the model portion of JTree to capture hierarchical
structures; look for my discussion of the UI portion sometime in early '99.
We'll begin with a brief look at the MVC architectural pattern and examine how it is integrated into Swing. Then, we'll move
into a detailed discussion of the Swing tree package and its interfaces. Along the way, we'll examine listeners and various
strategies for using the tree package. Finally, we'll bring our discussion to a close with a look at a simple program that
demonstrates Swing's tree capabilities in action.
We've got a lot to cover, so let's get started!
A brief look at the MVC pattern
Swing's Model-UI architecture is a variation of the MVC (Model-View-Controller) pattern (see Resources). The MVC pattern, first introduced in Smalltalk-80, was designed to reduce the programming effort required to build systems
that made use of multiple, synchronized presentations of the same data. The MVC consists of three distinct parts:
- The Model holds the data that is being represented.
- The View renders the data of the model. The view observes changes in the model and then renders the updated model data. Because there
is only a single central model, all views will remain coherent.
- The Controller listens to user events and controls the view on the data.
The MVC pattern prompts the following software qualities, which make it such a novel idea:
Extensibility -- It is common that the GUI of an application evolves much faster than its logic (the model). Using MVC, you may change
the GUI without changing the model.
Loose coupling -- The model publishes only an interface for notification. Each view implements this interface, but the model does not know,
nor, care about the view beyond this interface.
Pluggabilty -- We can add new views easily, without changing the model.
MVC meets Swing
The MVC design pattern is typically used for constructing user interfaces that may present multiple views of the same data.
The design pattern is extremely important because it completely de-couples data display from data representation. This means
that the user interface of an application can be completely changed -- even dynamically, under direction of the user -- without need for any change to the underlying data subsystem
of the application. As long as you can implement the model (TreeModel, more on this later), your view does not have to change.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
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