• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ToolTipText Code for AWT and Applet

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...except that this has nothing to do with Jdbc, I'll move it to the Swing/AWT forum for you.
Dave.
 
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic