Using JTree to display a hierarchy

In this tutorial we will take a look at displaying JTree information in a hierarchy. We will introduce the Tree Node object that works alongside JTree. As we go through the tutorial we will see how the Tree Node handles the tree data and the JTree takes care of displaying the information.

Download source code and art work
(you will need to provide an email address)

If we want to build a JTree with a hierarchy then we need to use Tree Nodes.  There are a two TreeNode interfaces provided in the swing package, TreeNode and MutableTreeNode as well as a concrete class DefaultMutableNode.   So, we could build our own implementation of TreeNode and implement one or both of the interfaces or we can, given our simple needs, build the hierarchy using the DefaultMutableTreeNode like this:

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.