JTree : Java Glossary

home page J words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all) ©1996-2010 Roedy Green, Canadian Mind Products
JTree
A Swing GUI 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 J tree.

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.

Learning More

Sun’s Javadoc on JTree class : available:
Sun’s Javadoc on TreeModel interface : available:
Sun’s Javadoc on DefaultTreeModel class : available:
Sun’s Javadoc on TreeNode interface : available:
Sun’s Javadoc on MutableTreeNode interface : available:
Sun’s Javadoc on DefaultMutableTreeNode class : available:
Sun’s Javadoc on TreePath class : available:
Sun’s Javadoc on TreeCellRenderer interface : available:
Sun’s Javadoc on TreeCellEditor interface : available:

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/jtree.html J:\mindprod\jgloss\jtree.html
CMP logofeedback Please email your feedback for publication, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.191.114]
You are visitor number 13,908.