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

Keywords/tags

java, swing, JTree, tree, tooltip, tool tip, help, text, hover, rollover, model, TreeModel, DefaultTreeModel, TreeNode, TreePath, ImageObserver, event

The source code

/* (swing1.1beta3) swing#948 */
package tame.examples;

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

/**
@author Nobuo Tamemasa
@version 1.0 11/09/98
*/
public class ToolTipTreeExample extends JFrame {
  public ToolTipTreeExample() {
    super("ToolTipTreeExample");
    String[][] strs = {{"swing", "boat"    },     // 0
		       {"platf", "paltform"},     // 1
		       {"basic", "fortran" },     // 2
		       {"metal", "heavy"   },     // 3
		       {"JTree", "three"  }};     // 4

    ToolTipTreeNode[] nodes = new ToolTipTreeNode[strs.length];
    for (int i=0;i<strs.length;i++) {
      nodes[i] = new ToolTipTreeNode(strs[i][0],strs[i][1]);
    }
    nodes[0].add(nodes[1]);
    nodes[1].add(nodes[2]);
    nodes[1].add(nodes[3]);
    nodes[0].add(nodes[4]);
    JTree tree = new JTree( nodes[0] ){
      public String getToolTipText(MouseEvent evt) {
	if (getRowForLocation(evt.getX(), evt.getY()) == -1) return null;
	TreePath curPath = getPathForLocation(evt.getX(), evt.getY());
	return ((ToolTipTreeNode)curPath.getLastPathComponent()).getToolTipText();
      }
    };
    tree.setToolTipText("");
    JScrollPane sp = new JScrollPane(tree);
    getContentPane().add(sp, BorderLayout.CENTER);
  }

  public static void main(String args[]) {
    ToolTipTreeExample frame = new ToolTipTreeExample();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {System.exit(0);}
    });
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}

/**
@version 1.0 11/09/98
@author Nobuo Tamemasa
*/
class ToolTipTreeNode extends DefaultMutableTreeNode {
  private String toolTipText;

  public ToolTipTreeNode(String str, String toolTipText) {
    super(str);
    this.toolTipText = toolTipText;
  }

  public String getToolTipText() {
    return toolTipText;
  }
}

Copyright © 1998-2009 DevDaily.com
All Rights Reserved.