Animated Icon Tree Example 2 : Tree « Swing Components « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing Components » TreeScreenshots 
Animated Icon Tree Example 2

// Example from http://www.crionics.com/products/opensource/faq/swing_ex/SwingExamples.html


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.ImageObserver;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 @version 1.0 06/20/99
 */
public class AnimatedIconTreeExample2 extends JFrame {
  final static String CAT = "cat";

  final static String DOG = "dog";

  public AnimatedIconTreeExample2() {
    super("AnimatedIconTreeExample2");
    String[] strs = "CARNIVORA"// 0
        "Felidae"// 1
        "Acinonyx jutatus  (cheetah)"// 2
        "Panthera leo  (lion)"// 3
        "Canidae"// 4
        "Canis lupus  (wolf)"// 5
        "Lycaon pictus  (lycaon)"// 6
        "Vulpes Vulpes  (fox)" }// 7

    IconNode[] nodes = new IconNode[strs.length];
    for (int i = 0; i < strs.length; i++) {
      nodes[inew IconNode(strs[i]);
    }
    nodes[0].add(nodes[1]);
    nodes[0].add(nodes[4]);
    nodes[1].add(nodes[2]);
    nodes[1].add(nodes[3]);
    nodes[4].add(nodes[5]);
    nodes[4].add(nodes[6]);
    nodes[4].add(nodes[7]);

    // If you want to share the ImageIcon with a node.

    nodes[2].setIconName(CAT);
    nodes[3].setIconName(CAT);
    nodes[5].setIconName(DOG);
    nodes[6].setIconName(DOG);
    nodes[7].setIconName(DOG);

    Hashtable icons = new Hashtable();
    icons.put(CAT, new ImageIcon("Java2sAnimation.gif"));
    icons.put(DOG, new ImageIcon("Java2sAnimation.gif"));

    JTree tree = new JTree(nodes[0]);
    tree.putClientProperty("JTree.icons", icons);
    tree.setCellRenderer(new IconNodeRenderer());
    setImageObserver(tree, nodes, icons);

    JScrollPane sp = new JScrollPane(tree);
    getContentPane().add(sp, BorderLayout.CENTER);
  }

  private void setImageObserver(JTree tree, IconNode[] nodes, Hashtable icons) {
    Hashtable observers = new Hashtable();

    for (int i = 0; i < nodes.length; i++) {
      ImageIcon icon = (ImageIconnodes[i].getIcon();
      if (icon != null) {
        Vector repaintNodes = new Vector();
        repaintNodes.addElement(nodes[i]);
        icon
            .setImageObserver(new NodeImageObserver(tree,
                repaintNodes));
      else {
        String iconName = nodes[i].getIconName();
        if (iconName != null) {
          Vector repaintNodes = (Vectorobservers.get(iconName);
          if (repaintNodes == null) {
            repaintNodes = new Vector();
            observers.put(iconName, repaintNodes);
          }
          repaintNodes.addElement(nodes[i]);
        }
      }
    }

    Enumeration e = observers.keys();
    while (e.hasMoreElements()) {
      String iconName = (Stringe.nextElement();
      Vector repaintNodes = (Vectorobservers.get(iconName);
      ImageIcon icon = (ImageIconicons.get(iconName);
      icon.setImageObserver(new NodeImageObserver(tree, repaintNodes));
    }
  }

  class NodeImageObserver implements ImageObserver {
    JTree tree;

    DefaultTreeModel model;

    Vector nodes;

    NodeImageObserver(JTree tree, Vector nodes) {
      this.tree = tree;
      this.model = (DefaultTreeModeltree.getModel();
      this.nodes = nodes;
    }

    public boolean imageUpdate(Image img, int flags, int x, int y, int w,
        int h) {
      if ((flags & (FRAMEBITS | ALLBITS)) != 0) {
        Enumeration e = nodes.elements();
        while (e.hasMoreElements()) {
          TreeNode node = (TreeNodee.nextElement();
          TreePath path = new TreePath(model.getPathToRoot(node));
          Rectangle rect = tree.getPathBounds(path);
          if (rect != null) {
            tree.repaint(rect);
          }
        }
      }
      return (flags & (ALLBITS | ABORT)) == 0;
    }
  }

  public static void main(String args[]) {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    catch (Exception evt) {}
  
    AnimatedIconTreeExample2 frame = new AnimatedIconTreeExample2();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    frame.setSize(280200);
    frame.setVisible(true);
  }
}

class IconNodeRenderer extends DefaultTreeCellRenderer {

  public Component getTreeCellRendererComponent(JTree tree, Object value,
      boolean sel, boolean expanded, boolean leaf, int row,
      boolean hasFocus) {

    super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
        row, hasFocus);

    Icon icon = ((IconNodevalue).getIcon();

    if (icon == null) {
      Hashtable icons = (Hashtabletree.getClientProperty("JTree.icons");
      String name = ((IconNodevalue).getIconName();
      if ((icons != null&& (name != null)) {
        icon = (Iconicons.get(name);
        if (icon != null) {
          setIcon(icon);
        }
      }
    else {
      setIcon(icon);
    }

    return this;
  }
}

class IconNode extends DefaultMutableTreeNode {

  protected Icon icon;

  protected String iconName;

  public IconNode() {
    this(null);
  }

  public IconNode(Object userObject) {
    this(userObject, true, null);
  }

  public IconNode(Object userObject, boolean allowsChildren, Icon icon) {
    super(userObject, allowsChildren);
    this.icon = icon;
  }

  public void setIcon(Icon icon) {
    this.icon = icon;
  }

  public Icon getIcon() {
    return icon;
  }

  public String getIconName() {
    if (iconName != null) {
      return iconName;
    else {
      String str = userObject.toString();
      int index = str.lastIndexOf(".");
      if (index != -1) {
        return str.substring(++index);
      else {
        return null;
      }
    }
  }

  public void setIconName(String name) {
    iconName = name;
  }

}

           
       
Related examples in the same category
1. Disabled Node Tree ExampleDisabled Node Tree Example
2. Disabled Node Tree Example 2Disabled Node Tree Example 2
3. Only Text Tree ExampleOnly Text Tree Example
4. Animated Icon Tree ExampleAnimated Icon Tree Example
5. MultiLine Tree ExampleMultiLine Tree Example
6. Wide Editor Tree ExampleWide Editor Tree Example
7. ToolTip Tree ExampleToolTip Tree Example
8. Checkbox Node Tree ExampleCheckbox Node Tree Example
9. Invisible Node Tree ExampleInvisible Node Tree Example
10. Icon Node Tree ExampleIcon Node Tree Example
11. Source code for building a tree in Swing
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.