wood burning stoves*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ToolTipText Code for AWT and Applet" Watch "ToolTipText Code for AWT and Applet" New topic
Author

ToolTipText Code for AWT and Applet

Suji R
Greenhorn

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
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
David O'Meara
Rancher

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.
Dave.
 
I agree. Here's the link: http://aspose.com/file-tools
 
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