Component Tree : Tree « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Swing JFC » TreeScreenshots 
Component Tree
Component Tree

/*
 * Copyright (c) 2000 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book (recommended),
 * visit http://www.davidflanagan.com/javaexamples2.
 */

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

/**
 * This class is a JTree subclass that displays the tree of AWT or Swing
 * component that make up a GUI.
 */
public class ComponentTree extends JTree {
  /**
   * All this constructor method has to do is set the TreeModel and
   * TreeCellRenderer objects for the tree. It is these classes (defined
   * below) that do all the real work.
   */
  public ComponentTree(Component c) {
    super(new ComponentTreeModel(c));
    setCellRenderer(new ComponentCellRenderer(getCellRenderer()));
  }

  /**
   * The TreeModel class puts hierarchical data in a form that the JTree can
   * display. This implementation interprets the containment hierarchy of a
   * Component for display by the ComponentTree class. Note that any kind of
   * Object can be a node in the tree, as long as the TreeModel knows how to
   * handle it.
   */
  static class ComponentTreeModel implements TreeModel {
    Component root; // The root object of the tree

    // Constructor: just remember the root object
    public ComponentTreeModel(Component root) {
      this.root = root;
    }

    // Return the root of the tree
    public Object getRoot() {
      return root;
    }

    // Is this node a leaf? (Leaf nodes are displayed differently by JTree)
    // Any node that isn't a container is a leaf, since they cannot have
    // children. We also define containers with no children as leaves.
    public boolean isLeaf(Object node) {
      if (!(node instanceof Container))
        return true;
      Container c = (Containernode;
      return c.getComponentCount() == 0;
    }

    // How many children does this node have?
    public int getChildCount(Object node) {
      if (node instanceof Container) {
        Container c = (Containernode;
        return c.getComponentCount();
      }
      return 0;
    }

    // Return the specified child of a parent node.
    public Object getChild(Object parent, int index) {
      if (parent instanceof Container) {
        Container c = (Containerparent;
        return c.getComponent(index);
      }
      return null;
    }

    // Return the index of the child node in the parent node
    public int getIndexOfChild(Object parent, Object child) {
      if (!(parent instanceof Container))
        return -1;
      Container c = (Containerparent;
      Component[] children = c.getComponents();
      if (children == null)
        return -1;
      for (int i = 0; i < children.length; i++) {
        if (children[i== child)
          return i;
      }
      return -1;
    }

    // This method is only required for editable trees, so it is not
    // implemented here.
    public void valueForPathChanged(TreePath path, Object newvalue) {
    }

    // This TreeModel never fires any events (since it is not editable)
    // so event listener registration methods are left unimplemented
    public void addTreeModelListener(TreeModelListener l) {
    }

    public void removeTreeModelListener(TreeModelListener l) {
    }
  }

  /**
   * A TreeCellRenderer displays each node of a tree. The default renderer
   * displays arbitrary Object nodes by calling their toString() method. The
   * Component.toString() method returns long strings with extraneous
   * information. Therefore, we use this "wrapper" implementation of
   * TreeCellRenderer to convert nodes from Component objects to useful String
   * values before passing those String values on to the default renderer.
   */
  static class ComponentCellRenderer implements TreeCellRenderer {
    TreeCellRenderer renderer; // The renderer we are a wrapper for

    // Constructor: just remember the renderer
    public ComponentCellRenderer(TreeCellRenderer renderer) {
      this.renderer = renderer;
    }

    // This is the only TreeCellRenderer method.
    // Compute the string to display, and pass it to the wrapped renderer
    public Component getTreeCellRendererComponent(JTree tree, Object value,
        boolean selected, boolean expanded, boolean leaf, int row,
        boolean hasFocus) {
      String newvalue = value.getClass().getName()// Component type
      String name = ((Componentvalue).getName()// Component name
      if (name != null)
        newvalue += " (" + name + ")"// unless null
      // Use the wrapped renderer object to do the real work
      return renderer.getTreeCellRendererComponent(tree, newvalue,
          selected, expanded, leaf, row, hasFocus);
    }
  }

  /**
   * This main() method demonstrates the use of the ComponentTree class: it
   * puts a ComponentTree component in a Frame, and uses the ComponentTree to
   * display its own GUI hierarchy. It also adds a TreeSelectionListener to
   * display additional information about each component as it is selected
   */
  public static void main(String[] args) {
    // Create a frame for the demo, and handle window close requests
    JFrame frame = new JFrame("ComponentTree Demo");
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    // Create a scroll pane and a "message line" and add them to the
    // center and bottom of the frame.
    JScrollPane scrollpane = new JScrollPane();
    final JLabel msgline = new JLabel(" ");
    frame.getContentPane().add(scrollpane, BorderLayout.CENTER);
    frame.getContentPane().add(msgline, BorderLayout.SOUTH);

    // Now create the ComponentTree object, specifying the frame as the
    // component whose tree is to be displayed. Also set the tree's font.
    JTree tree = new ComponentTree(frame);
    tree.setFont(new Font("SansSerif", Font.BOLD, 12));

    // Only allow a single item in the tree to be selected at once
    tree.getSelectionModel().setSelectionMode(
        TreeSelectionModel.SINGLE_TREE_SELECTION);

    // Add an event listener for notifications when
    // the tree selection state changes.
    tree.addTreeSelectionListener(new TreeSelectionListener() {
      public void valueChanged(TreeSelectionEvent e) {
        // Tree selections are referred to by "path"
        // We only care about the last node in the path
        TreePath path = e.getPath();
        Component c = (Componentpath.getLastPathComponent();
        // Now we know what component was selected, so
        // display some information about it in the message line
        if (c.isShowing()) {
          Point p = c.getLocationOnScreen();
          msgline.setText("x: " + p.x + "  y: " + p.y + "  width: "
              + c.getWidth() "  height: " + c.getHeight());
        else {
          msgline.setText("component is not showing");
        }
      }
    });

    // Now that we've set up the tree, add it to the scrollpane
    scrollpane.setViewportView(tree);

    // Finally, set the size of the main window, and pop it up.
    frame.setSize(600400);
    frame.setVisible(true);
  }
}

           
       
Related examples in the same category
1. Simple Swing tree. Trees can be vastly more complexSimple Swing tree. Trees can be vastly more complex
2. Tree SampleTree Sample
3. Genealogy TreeGenealogy Tree
4. Tree LinesTree Lines
5. Node Tree SampleNode Tree Sample
6. Tree Cell RendererTree Cell Renderer
7. CheckBox Node Tree SampleCheckBox Node Tree Sample
8. ComboTreeSample: TreeCellRendererComboTreeSample: TreeCellRenderer
9. Tree edit Leaf SampleTree edit Leaf Sample
10. Swing JTree exampleSwing JTree example
11. Simple JFC JTree Simple application, showing default tree contentsSimple JFC JTree Simple application, showing default tree contents
12. Simple JFC JTree demoSimple JFC JTree demo
13. Display a file system in a JTree viewDisplay a file system in a JTree view
14. Class BrowserClass Browser
15. Class Tree Class Tree
16. Tree Edit TestTree Edit Test
17. Simple Tree DemoSimple Tree Demo
18. File TreeFile Tree
19. File Tree with Popup MenuFile Tree with Popup Menu
20. File Tree with TooltipsFile Tree with Tooltips
21. Ancestor Tree with IconsAncestor Tree with Icons
22. File Tree DemoFile Tree Demo
23. Tree Icon DemoTree Icon Demo
24. Tree Icon Demo 2Tree Icon Demo 2
25. Tree Demo 2Tree Demo 2
26. Tree Expand Event DemoTree Expand Event Demo
27. Tree: Drag and DropTree: Drag and Drop
28. Tree open IconTree open Icon
29. Traverse TreeTraverse Tree
30. Tree Array SampleTree Array Sample
31. Tree Expand SampleTree Expand Sample
32. Tree Line SampleTree Line Sample
33. Tree Selection RowTree Selection Row
34. Tree Util SampleTree Util Sample
35. Tree ToolTipsTree ToolTips
36. Tree Changed RendererTree Changed Renderer
37. Tree Expand Event Demo 2Tree Expand Event Demo 2
38. A tree with componentA tree with component
39. A sample component for dragging and dropping a collection of files into a tree.A sample component for dragging and dropping a collection of files into a tree.
40. DnD (drag and drop)JTree code DnD (drag and drop)JTree code
41. Build a tree and populate it with custom renderers and editorsBuild a tree and populate it with custom renderers and editors
42. Build a tree and populate it from hashtablesBuild a tree and populate it from hashtables
43. A tree model using the SortTreeModel with a File hierarchy as inputA tree model using the SortTreeModel with a File hierarchy as input
44. A simple test to see how we can build a tree and populate itA simple test to see how we can build a tree and populate it
45. Installs custom iconsInstalls custom icons
46. Build a tree and customize its iconsBuild a tree and customize its icons
47. Displaying Hierarchical Data within a JTreeDisplaying Hierarchical Data within a JTree
48. Add and remove tree Node and expand the tree node
49. File System Tree
w__w_w__.___ja_v__a2s__.___c___om__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.