devdaily home | java | perl | unix | directory | blog

What this is

This file is part of the Tame Swing collection that we have compiled. All of the Java source code in these examples was created by Nobuo Tamemasa. As these examples have become hard to find on the internet, we have put them in one place here, supported by minimal advertising. In time we will include images of each running sample Java/Swing application.

The intent of having these files here is to help you learn Java and Swing by example, and Tame certainly created some terrific examples of the power of the Swing framework.

Other links


java, swing, JTree, ImageIcon, tree, observer, icon, image, animated, model, DefaultTreeModel, TreeModel, TreeNode, TreePath, IconNode

The source code

/* (swing1.1.1beta2)*/
package tame.examples;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.ImageObserver;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import tame.tree.IconNode;
import tame.tree.IconNodeRenderer;

@author Nobuo Tamemasa
@version 1.0 06/20/99
public class AnimatedIconTreeExample extends JFrame {

  public AnimatedIconTreeExample() {
    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[i] = new IconNode(strs[i]);

    nodes[2].setIcon(new ImageIcon("images/3-119.gif"));
    nodes[3].setIcon(new ImageIcon("images/3-119.gif"));
    nodes[5].setIcon(new ImageIcon("images/3-6.gif"));
    nodes[6].setIcon(new ImageIcon("images/3-6.gif"));
    nodes[7].setIcon(new ImageIcon("images/3-6.gif"));

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

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

  private void setImageObserver(JTree tree,IconNode[] nodes) {
    for (int i=0;i<nodes.length;i++) {
      ImageIcon icon = (ImageIcon)nodes[i].getIcon();
      if (icon != null) {
        icon.setImageObserver(new NodeImageObserver(tree, nodes[i]));

  class NodeImageObserver implements ImageObserver {
    JTree            tree;
    DefaultTreeModel model;
    TreeNode         node;

    NodeImageObserver(JTree tree,TreeNode node) {
      this.tree  = tree;
      this.model = (DefaultTreeModel)tree.getModel();
      this.node  = node;

    public boolean imageUpdate(Image img, int flags,
			       int x, int y, int w, int h) {
      if ((flags & (FRAMEBITS|ALLBITS)) != 0) {
        TreePath path = new TreePath(model.getPathToRoot(node));
        Rectangle rect = tree.getPathBounds(path);
        if (rect != null) {
      return (flags & (ALLBITS|ABORT)) == 0;

  public static void main(String args[]) {
    AnimatedIconTreeExample frame = new AnimatedIconTreeExample();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {System.exit(0);}
    frame.setSize(280, 200);

Copyright 1998-2009
All Rights Reserved.