JTree : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

JTree
A Swing GUI (Graphic User Interface) widget for displaying trees with nodes and lines going between them. You can independently hide various levels of detail in the tree, including the root, allowing the display to collapse and expand various parts of the tree. You can permanently hide the root with: JTree.setRootVisible ( false );

Jtree also lets you edit data about the nodes, add new nodes and prune. You use the TreeModel interface implemented by DefaultTreeModel to manage your tree data. You change the tree by poking the TreeModel, not by poking the TreeNodes directly. You can also use TreeNodes independent of a display, though most algorithmic tree handling is done with specialised classes.

Note that it is spelled JTree not Jtree

Debugging

The most common bugs in using JTrees are: JTrees are tricky. I strongly suggest buying a textbook that covers them rather than trying to figure it out all on your own from the Javadoc. Much of what I have discovered about JTables also applies to JTrees.

Sorting

I have never actually done this, but if I wanted to sort a JTree, here is how I would go about it:
  1. Do a breadth first traversal of the tree.
  2. At each node extract a list of the childen into an array.
  3. Sort the array.
  4. If all is well, that part of the tree is already in order. If not, delete the children, leaving the early ones (if any) which are already in order, then re-add the out of order ones in sorted order. Don’t fire any change events yet.
  5. When you have sorted the entire tree, fire a tree structure change event on the root.

Classes

JTree Classes
Class Purpose
JTree master GUI component
DefaultTreeModel used this code as a base to hold all your tree data.
TreeNode Interface to write code for a node if the user may not modify.
MutableTreeNode Inteface to write code for a node the user can modify either by adding or removing children or by changing the value associated with the node. Normally use DefaultMutableTreeNode.
DefaultMutableTreeNode use this code as a base to hold data for one node of your TreeModel
TreePath represents a hierarchical chain through the tree.
TreeCellRenderer Interface for your code to display a node.
DefaultTreeCellRenderer using this code as a base to write your own cell renderer.
TreeCellEditor Interface for your code to allow user to edit a node.
DefaultTreeCellEditor using this code as a base to write your own cell editor.
TreeSelectionListener interface to write your own code to respond to user selecting/deselecting a node.

Learning More

Oracle’s Javadoc on JTree class : available:
Oracle’s Javadoc on TreeModel interface : available:
Oracle’s Javadoc on DefaultTreeModel class : available:
Oracle’s Javadoc on TreeNode interface : available:
Oracle’s Javadoc on MutableTreeNode interface : available:
Oracle’s Javadoc on DefaultMutableTreeNode class : available:
Oracle’s Javadoc on TreePath class : available:
Oracle’s Javadoc on TreeCellRenderer interface : available:
Oracle’s Javadoc on DefaultTreeCellRenderer class : available:
Oracle’s Javadoc on TreeCellEditor interface : available:
Oracle’s Javadoc on DefaultTreeCellEditor class : available:
Oracle’s Javadoc on TreeSelectionListener interface : available:

This page is posted
on the web at:

http://mindprod.com/jgloss/jtree.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\jtree.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[3.12.34.211]
You are visitor number