Suji R

Greenhorn
+ Follow
since Nov 16, 2001
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Suji R

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="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);
}
}
Thankyou Very Much
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="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);
}
}
Thankyou Very Much
22 years ago