How do I display different tooltip text for each entry in a JList?

John Zukowski

Besides keeping track of what tooltip text to display for each entry, there are two major steps involved:

  1. Step one involves registering the JList with the ToolTipManager so that it knows to fetch tool tip text to display for the component. This is normally automatically done for you when call setToolTipText(). However, since you don't call setToolTipText() on the JList, you need to register the component yourself:
  2. Once the component is registered, the manager will call the public String getToolTipText(MouseEvent event) method to display the tooltip when appropriate. You would need to override this method to display the appropriate tooltip text for the cell. How you manage the tips is up to you, but basically the method would look something like the following:
    public String getToolTipText(MouseEvent event) {
      Point p = event.getPoint();
      int location = locationToIndex(p);
      String tip = getTipForLocation(location)
      return tip;