tooltip


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: tooltip

Hybrid View

  1. #1
    zhouhao Guest

    tooltip


    Hi,

    How can I add a tooltip when I move the mouse in my applet?

    I'm using awt.

    Thanks,

    Zhou

  2. #2
    Chris Birkett Guest

    Re: tooltip


    You need to create an instance of the Window class. This creates a window
    without any title bar or border.

    "zhouhao" <zhouhao@csksoftware.com> wrote:
    >
    >Hi,
    >
    >How can I add a tooltip when I move the mouse in my applet?
    >
    >I'm using awt.
    >
    >Thanks,
    >
    >Zhou



  3. #3
    Rajesh Guest

    Re: tooltip

    Dear zhouhao,
    Here is the code that you are searching for.
    Test these 2 files. and let me know the result.
    You can use these for awt as well as for applets.
    Thank you very much.
    Cheers.

    ======================
    ToolTip.java

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;

    public class ToolTip extends Canvas {

    protected String tip;
    protected Component owner;

    private Container mainContainer;
    private LayoutManager mainLayout;

    private boolean shown;

    private final int VERTICAL_OFFSET = 30;
    private final int HORIZONTAL_ENLARGE = 10;

    public ToolTip(String tip, Component owner) {
    this.tip = tip;
    this.owner = owner;
    owner.addMouseListener(new MAdapter());
    setBackground(new Color(255,255,220));
    }


    public void paint(Graphics g) {
    g.drawRect(0,0,getSize().width -1, getSize().height -1);
    g.drawString(tip, 3, getSize().height - 3);
    }

    private void addToolTip() {
    mainContainer.setLayout(null);

    FontMetrics fm = getFontMetrics(owner.getFont());
    setSize(fm.stringWidth(tip) + HORIZONTAL_ENLARGE, fm.getHeight());

    setLocation((owner.getLocationOnScreen().x -
    mainContainer.getLocationOnScreen().x) ,
    (owner.getLocationOnScreen().y - mainContainer.getLocationOnScreen().y
    + VERTICAL_OFFSET));

    // correction, whole tool tip must be visible
    if (mainContainer.getSize().width < ( getLocation().x + getSize().width ))
    {
    setLocation(mainContainer.getSize().width - getSize().width,
    getLocation().y);
    }
    mainContainer.add(this, 0);
    mainContainer.validate();
    repaint();
    shown = true;
    }


    private void removeToolTip() {
    if (shown) {
    mainContainer.remove(0);
    mainContainer.setLayout(mainLayout);
    mainContainer.validate();
    }
    shown = false;
    }

    private void findMainContainer() {
    Container parent = owner.getParent();
    while (true) {
    if ((parent instanceof Applet) || (parent instanceof Frame)) {
    mainContainer = parent;
    break;
    } else {
    parent = parent.getParent();
    }
    }
    mainLayout = mainContainer.getLayout();
    }

    class MAdapter extends MouseAdapter {
    public void mouseEntered(MouseEvent me) {
    findMainContainer();
    addToolTip();
    }
    public void mouseExited(MouseEvent me) {
    removeToolTip();
    }
    public void mousePressed(MouseEvent me) {
    removeToolTip();
    }
    }
    }
    ======================
    /*
    <applet code="ToolTipTest.class" width=300 height=300></applet>

    */

    import java.awt.*;
    import java.awt.event.*;

    public class ToolTipTest extends java.applet.Applet {

    private Label myLabel;
    private Button myButton;
    private TextField myTextField;

    public void init() {

    myLabel = new Label("Hello world!");
    new ToolTip("I say: Hello world!", myLabel);

    myButton = new Button("Press");
    new ToolTip("It's working !", myButton);

    myTextField = new TextField(10);
    new ToolTip("Tip for this field", myTextField);

    add(myLabel);
    add(myButton);
    add(myTextField);
    }
    }




    ======================
    "zhouhao" <zhouhao@csksoftware.com> wrote in message
    news:3c0f294e$1@147.208.176.211...
    >
    > Hi,
    >
    > How can I add a tooltip when I move the mouse in my applet?
    >
    > I'm using awt.
    >
    > Thanks,
    >
    > Zhou




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center