Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ToolTipText Code for AWT and Applet

Status
Not open for further replies.

sujitopics

Programmer
Oct 9, 2001
69
KR
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
Yours
Suji

// 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() {
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();
}
}
}


// Second Program

/*
<applet code=&quot;ToolTipTest.class&quot; 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(&quot;Hello world!&quot;);
new ToolTip(&quot;I say: Hello world!&quot;, myLabel);

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

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

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

Thankyou Very Much
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top