This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes ToolTipText Code for AWT and Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ToolTipText Code for AWT and Applet" Watch "ToolTipText Code for AWT and Applet" New topic

ToolTipText Code for AWT and Applet

Suji R

Joined: Nov 16, 2001
Posts: 2
Hi Friends
This is the coding [2 files ] for ToolTipText.
You can use this one for AWT applications or for Applets.
If you have any doubts please give reply
Thankyou VeryMuch
// Two Programs
// First Program
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() {

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);
shown = true;

private void removeToolTip() {
if (shown) {
shown = false;
private void findMainContainer() {
Container parent = owner.getParent();
while (true) {
if ((parent instanceof Applet) | | (parent instanceof Frame)) {
mainContainer = parent;
} else {
parent = parent.getParent();
mainLayout = mainContainer.getLayout();
class MAdapter extends MouseAdapter {
public void mouseEntered(MouseEvent me) {
public void mouseExited(MouseEvent me) {
public void mousePressed(MouseEvent me) {

// Second Program
<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);

Thankyou Very Much
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

...except that this has nothing to do with Jdbc, I'll move it to the Swing/AWT forum for you.
I agree. Here's the link:
subject: ToolTipText Code for AWT and Applet
Similar Threads
ToolTipText for JLabel with in a Box (renderer for JTree Node)
TooltipText on click
help me on this simple applet
How to catch event on label
ToolTipText Code for AWT and Applet