Technical Support
Discussion Forum
Online Training
Technical Articles
Bug Parade
JDC Resources
DukeDollars
Early Access
Toolbar
Community Process
Technology Sites

JDC Home Page

Solaris Developer Connection

Java Technology
java.sun.com

Java Industry Connection

Log Out

Online Training
SearchFAQContact Us

Magercise 1
Tree Views

[Help | API Docs | Short Course | Magercises]

Magercise 1 Prerequisites

Fundamentals of Swing: Part I, Magercises

Skeleton Code

Swing provides a nice "treeview" component, JTree, that displays a hierarchy of other components. Each node of the tree implements the TreeNode interface, where the default note class provided is DefaultMutableTreeNode. In this magercise, you will create a simple class hierarchy view containing some of the classes in the new Swing library. You'll display the tree in a scrollable pane in case the tree becomes bigger than the surrounding frame. When the user selects a node, you should print out both the specific location of the node and the path to get there.

Tasks:

  1. Starting with the skeleton, import Swing's tree package.

  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");
    

  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.

  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.

  5. Once you've defined a TreeSelectionListener class, create an instance of it.

  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.

  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.

  8. Change the lineStyle client property of the tree to Angled.

  9. Associate the TreeSelectionListener to the JTree if it is non-null.

  10. Then, create a JScrollPane object, and place the tree in it.

  11. Save everything and compile the program. Then run it to see the results.

Result

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution

The following Java™ source files represent a solution to this Magercise:

Magercise 2

Short Course

About This Short Course

Copyright © 1998-1999 MageLang Institute. All Rights Reserved.





Questions?
29-Mar-99
Copyright © 1996-1999 Sun Microsystems Inc.
All Rights Reserved. Legal Terms. Privacy Policy.
Sun Microsystems