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, example, sample, source, custom, list, jlist, tool, tip, tooltip, help, event

The source code

/* (swing1.1.1) */
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.JList;
import javax.swing.JScrollPane;

/**
@author Nobuo Tamemasa
@version 1.0 08/26/99
*/
public class ToolTipListExample extends JFrame {

  public ToolTipListExample() {
    super("ToolTip Example");

    String[][] strs = {{"Acinonyx jutatus","Cheetah"},
                       {"Panthera leo"    ,"Lion"   },
                       {"Canis lupus"     ,"Wolf"   },
                       {"Lycaon pictus"   ,"Llycaon"},
                       {"Vulpes Vulpes"   ,"Fox"    }};

    JList list = new JList( createItems(strs) ) {
      public String getToolTipText(MouseEvent e) {
        int index = locationToIndex(e.getPoint());
        if (-1 < index) {
          ToolTipItem item = (ToolTipItem)getModel().getElementAt(index);
          return item.getToolTipText();
        } else {
          //return super.getToolTipText();
          return null;
        }
      }
    };
    list.setToolTipText("");

    getContentPane().add(new JScrollPane(list), BorderLayout.CENTER);
  }

  Object[] createItems(String[][] strs) {
    ToolTipItem[] items = new ToolTipItem[strs.length];
    for (int i=0;i<strs.length;i++) {
      items[i] = new ToolTipItem(strs[i][0], strs[i][1]);
    }
    return items;
  }

  class ToolTipItem {
    String  obj;
    String  toolTipText;

    public ToolTipItem(String obj, String text) {
      this.obj = obj;
      this.toolTipText = text;
    }

    public String getToolTipText() {
      return toolTipText;
    }

    public String toString() {
      return obj;
    }
  }

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