ToolTip on a Layer

From: Chinh Tang <ctang@teloptica.com>
Date: Thu Jul 18 2002 - 11:19:09 EDT

Hi all,
I thought it would be nice if I share my version of a Tooltip that can
be used on a layer. The CustomToolTip listed at the bottom is a simple
JWindow and can be modified to contain any Component or JComponent and
can be used pretty much anywhere.

Have fun customizing it,
-Chinh

This is how you would use it on a layer that is listening to a mouseMove():

   CustomToolTip customToolTip = new CustomToolTip();

   public void mouseMoved(MouseEvent e)
   {
        Point pt = e.getPoint();
        //somehow in your implementation, get the OMGraphicList
        OMGraphicList graphicList = getOMGraphicList();
        //find the node at this point
        OMGraphic om = graphicList.findClosest(pt.x,pt.y,2);
 
        if (om!=null)
           customToolTip.showAt(om.getAppObject().toString(), this,
ptMouse.x,ptMouse.y);
        else
           customToolTip.setVisible(false);
    }

/**////////////////////////////////////////////////////////////////////////////////////////////////////////
* tooltip that can be used anywhere
**/////////////////////////////////////////////////////////////////////////////////////////////////////////
public class CustomToolTip extends JWindow
   {
      JLabel label = new JLabel("");
      public CustomToolTip()
      {
         this.setVisible(false);
         Container con = this.getContentPane();
         con.add(label);
         label.setBorder(BorderFactory.createRaisedBevelBorder());
         con.setBackground(Color.yellow.brighter().brighter());
         con.setForeground(Color.black);
         label.setOpaque(false);
      }

      private static Frame getFrame(Component c)
      {
         Component w = c;
         while(!(w instanceof Frame) && (w!=null)) {
             w = w.getParent();
         }
         return (Frame)w;
      }

      public void showAt(String s, Component invoker, int x, int y)
      {
          int width=100, int height=30;
          label.setText(s);
          if (invoker != null)
          {
             Point invokerOrigin = invoker.getLocationOnScreen();
             x += invokerOrigin.x;
             y += invokerOrigin.y;
            Frame frame = getFrame(invoker);
            Graphics g = frame.getGraphics();
            Rectangle2D rec = g.getFontMetrics().getStringBounds(s,g);
            width = rec.getWidth();
            height = rec.getHeight();
          }
         
          this.setBounds(x+15,y+15,width+7, height+7);
          this.setVisible(true);
          this.toFront();
      }
   }

--
[To unsubscribe to this list send an email to "majdart@bbn.com"
with the following text in the BODY of the message "unsubscribe openmap-users"]
Received on Thu Jul 18 11:21:52 2002

This archive was generated by hypermail 2.1.8 : Thu May 12 2005 - 07:18:33 EDT