Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
Print Button
 
Training Index

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 JavaTM source files represent a solution to this Magercise:

Magercise 2

Short Course

About This Short Course

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


Print Button
[ This page was updated: 21-Sep-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary | Feedback | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.