-
tooltip
Hi,
How can I add a tooltip when I move the mouse in my applet?
I'm using awt.
Thanks,
Zhou
-
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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks