Class 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 
Class Tree
Class Tree

/**
 @version 1.00 1999-07-17
 @author Cay Horstmann
 */

import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.Modifier;
import java.util.Enumeration;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

public class ClassTree {
  public static void main(String[] args) {
    JFrame frame = new ClassTreeFrame();
    frame.show();
  }
}

class ClassTreeFrame extends JFrame implements ActionListener {
  public ClassTreeFrame() {
    setTitle("ClassTree");
    setSize(300200);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    // the root of the class tree is Object
    root = new DefaultMutableTreeNode(java.lang.Object.class);
    model = new DefaultTreeModel(root);
    tree = new JTree(model);

    // add this class to populate the tree with some data
    addClass(getClass());

    // set up node icons
    ClassNameTreeCellRenderer renderer = new ClassNameTreeCellRenderer();
    //  renderer.setClosedIcon(new ImageIcon("red-ball.gif"));
    // renderer.setOpenIcon(new ImageIcon("yellow-ball.gif"));
    // renderer.setLeafIcon(new ImageIcon("blue-ball.gif"));
    tree.setCellRenderer(renderer);

    Container contentPane = getContentPane();
    contentPane.add(new JScrollPane(tree)"Center");

    // new class names are typed into this text field
    textField = new JTextField();
    textField.addActionListener(this);
    contentPane.add(textField, "South");
  }

  public void actionPerformed(ActionEvent event) { // add the class whose name
                           // is in the text field
    try {
      String text = textField.getText();
      addClass(Class.forName(text));
      // clear text field to indicate success
      textField.setText("");
    catch (ClassNotFoundException e) {
      Toolkit.getDefaultToolkit().beep();
    }
  }

  public DefaultMutableTreeNode findUserObject(Object obj) { // find the node
                                 // containing a
                                 // user object
    Enumeration e = root.breadthFirstEnumeration();
    while (e.hasMoreElements()) {
      DefaultMutableTreeNode node = (DefaultMutableTreeNodee
          .nextElement();
      if (node.getUserObject().equals(obj))
        return node;
    }
    return null;
  }

  public DefaultMutableTreeNode addClass(Class c) { // add a new class to the
                            // tree

    // skip non-class types
    if (c.isInterface() || c.isPrimitive())
      return null;

    // if the class is already in the tree, return its node
    DefaultMutableTreeNode node = findUserObject(c);
    if (node != null)
      return node;

    // class isn't present--first add class parent recursively

    Class s = c.getSuperclass();

    DefaultMutableTreeNode parent;
    if (s == null)
      parent = root;
    else
      parent = addClass(s);

    // add the class as a child to the parent
    DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(c);
    model.insertNodeInto(newNode, parent, parent.getChildCount());

    // make node visible
    TreePath path = new TreePath(model.getPathToRoot(newNode));
    tree.makeVisible(path);

    return newNode;
  }

  private DefaultMutableTreeNode root;

  private DefaultTreeModel model;

  private JTree tree;

  private JTextField textField;
}

class ClassNameTreeCellRenderer extends DefaultTreeCellRenderer {
  public Component getTreeCellRendererComponent(JTree tree, Object value,
      boolean selected, boolean expanded, boolean leaf, int row,
      boolean hasFocus) {
    super.getTreeCellRendererComponent(tree, value, selected, expanded,
        leaf, row, hasFocus);
    // get the user object
    DefaultMutableTreeNode node = (DefaultMutableTreeNodevalue;
    Class c = (Classnode.getUserObject();

    // the first time, derive italic font from plain font
    if (plainFont == null) {
      plainFont = getFont();
      /*
       * the tree cell renderer is sometimes called with a label that has
       * a null font
       */
      if (plainFont != null)
        italicFont = plainFont.deriveFont(Font.ITALIC);
    }

    // set font to italic if the class is abstract
    if ((c.getModifiers() & Modifier.ABSTRACT== 0)
      setFont(plainFont);
    else
      setFont(italicFont);
    return this;
  }

  private Font plainFont = null;

  private Font italicFont = null;
};



           
       
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. Component TreeComponent Tree
12. Simple JFC JTree Simple application, showing default tree contentsSimple JFC JTree Simple application, showing default tree contents
13. Simple JFC JTree demoSimple JFC JTree demo
14. Display a file system in a JTree viewDisplay a file system in a JTree view
15. Class BrowserClass Browser
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__a__2s.___c_o___m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.