Help is available for each task, or you can go straight to
the solution source code.
Task 1
Starting with the
skeleton, import Swing's tree package.
import javax.swing.tree.*;
Task 2
Next, create a set of DefaultMutableTreeNode objects in
the TreeExample constructor, one for each node in the class
hierarchy:
Component
Container
Box
JComponent
AbstractButton
JButton
JMenuItem
JToggleButton
JLabel
... [literally "..."]
Use constructs such as:
DefaultMutableTreeNode component =
new DefaultMutableTreeNode("Component");
DefaultMutableTreeNode component =
new DefaultMutableTreeNode("Component");
DefaultMutableTreeNode container =
new DefaultMutableTreeNode("Container");
DefaultMutableTreeNode box =
new DefaultMutableTreeNode("Box");
DefaultMutableTreeNode jComponent =
new DefaultMutableTreeNode("JComponent");
DefaultMutableTreeNode abstractButton =
new DefaultMutableTreeNode("AbstractButton");
DefaultMutableTreeNode jButton =
new DefaultMutableTreeNode("JButton");
DefaultMutableTreeNode jMenuItem =
new DefaultMutableTreeNode("JMenuItem");
DefaultMutableTreeNode jToggle =
new DefaultMutableTreeNode("JToggleButton");
DefaultMutableTreeNode jLabel =
new DefaultMutableTreeNode("JLabel");
DefaultMutableTreeNode etc =
new DefaultMutableTreeNode("...");
Task 3
Once you've created all the nodes, add them to each other to construct the
hierarchy.
DefaultMutableTreeNode contains an add
method that defines the children of a node.
component.add(container);
container.add(box);
container.add(jComponent);
jComponent.add(abstractButton);
abstractButton.add(jButton);
abstractButton.add(jMenuItem);
abstractButton.add(jToggle);
jComponent.add(jLabel);
jComponent.add(etc);
Task 4
Once you have the hierarchy setup, create a class that implements
TreeSelectionListener . Have it print the label of the selected
cell and the full path to the cell.
Import the javax.swing.event package.
Define the class similar to the following:
class MyTreeSelectionListener
implements TreeSelectionListener {
public void valueChanged (
TreeSelectionEvent event) {
TreePath path = event.getPath();
System.out.println ("Selected: " +
path.getLastPathComponent());
Object elements[] = path.getPath();
for (int i=0; i<elements.length; i++) {
System.out.print ("->" + elements[i]);
}
System.out.println ();
}
}
Task 5
Once you've defined a TreeSelectionListener class, create an
instance of it.
TreeSelectionListener listener =
new MyTreeSelectionListener();
Task 6
Create a TreePanel and pass it the root of your component
hierarchy tree and the TreeSelectionListener . Add the panel
to the frame. You'll finish up the TreePanel
description next.
TreePanel tp = new TreePanel(component, listener);
Container content = getContentPane();
content.add(tp, BorderLayout.CENTER);
Task 7
The TreePanel class will offer the visual
representation of the tree. Modify the constructor of
TreePanel so
that it creates a JTree called tree
attached to the root passed in to the constructor.
JTree tree = new JTree(root);
Task 8
Change the lineStyle client property of the tree to
Angled .
tree.putClientProperty("JTree.lineStyle", "Angled");
Task 9
Associate the TreeSelectionListener to the JTree
if it is non-null.
if (listener != null)
tree.addTreeSelectionListener (listener);
Task 10
Then, create a JScrollPane object, and place the tree in it.
JScrollPane sp = new JScrollPane(tree);
Task 11
Save everything and compile the program. Then run it to see the results.
javac TreeExample.java
java TreeExample
The dependency checker automatically determines that TreePanel.java
needs to be compiled.
Copyright © 1998-1999
MageLang Institute.
All Rights Reserved.
|