GUI Changes: The AWT Grows Up |
With theJTree
class, you can display hierarchical data.JTree
doesn't actually contain your data; it's simply a view of the data. Here's a picture of a tree:
As the preceding figure shows,
JTree
displays its data vertically. Each row contains exactly one item of data (called a node). Every tree has a root node (called Root in the above figure) from which all nodes descend. Nodes that can't have children are called leaf nodes. In the above figure, the look-and-feel marks leaf nodes with a circle.Non-leaf nodes can have any number of children, or even no children. In the above figure, the look-and-feel marks non-leaf nodes by drawing a folder. Typically, the user can expand and unexpand non-leaf nodes -- making their children visible or invisible -- by clicking them. By default, non-leaf nodes start out unexpanded.
When you initialize a tree, you create a
TreeNode
instance for each node in the tree, including the root. To make a node a leaf, you invokesetAllowsChildren(false)
on it.When data values change -- for example, nodes are added -- you [the data model?] can let the
JTree[Model?]
know there's new data by invoking one of theTreeModelListener
methods. TheJTree
then displays the new data. [CHECK ALL THIS!]Here is a picture of an application that displays a tree in a scroll pane:
Try this:
- Compile and run the application. The source file is
TreeDemo.java
.
See Getting Started with Swing if you need help.- Expand a node.
If you're using the Basic look and feel, you do this by clicking the + sign to the left of the item.- Select a node.
If you're using the Basic look and feel, you do this by clicking the text for the node or the icon just to the left.
Below is the code from
TreeDemo.java
that implements the tree in the previous example.[API discussion to follow. Discuss renderer options.]//In initialization code in aJPanel
subclass: //Create the nodes. TreeNode top = new TreeNode("The Java Series"); TreeNode category; TreeNode book; category = new TreeNode("Books for Java Programmers"); top.add(category); //Tutorial book = new TreeNode("The Java Tutorial: Object-Oriented Programming for the Internet"); book.add(new TreeNode("Mary Campione")); book.add(new TreeNode("Kathy Walrath")); category.add(book); ...//Do the same for each book in the category... category = new TreeNode("Books for Java Implementers"); top.add(category); //VM book = new TreeNode("The Java Virtual Machine Specification"); book.add(new TreeNode("Tim Lindholm")); book.add(new TreeNode("Frank Yellin")); category.add(book); ...//Do the same for each book in the category... //Make all child-free nodes leaves. makeLeaves(top); JTree tree = new JTree(top); . . . //This method will be added to the TreeNode API in a future release. protected void makeLeaves(TreeNode top) { Enumeration childEnum = top.preorderEnumeration(); TreeNode descendant; while(childEnum.hasMoreElements()) { descendant = (TreeNode)childEnum.nextElement(); if(descendant.getChildCount() == 0) descendant.setAllowsChildren(false); } }
GUI Changes: The AWT Grows Up |