What is meant by tooltip text ? Explain application with proper java code.
Java Swing ToolTip
We can add tooltip text to almost all the components of Java Swing by using the following method setToolTipText(String s). This method sets the tooltip of the component to the specified string s. When the cursor enters the boundary of that component a popup appears and text is displayed.
Java Swing ToolTip Methods used:
getToolTipText() : returns the tooltip text for that component .
setToolTipText(String s) : sets the tooltip text for the component .
getToolTipText(MouseEvent e): returns the same value returned by getToolTipText(). Multi-part components such as JTabbedPane, JTable, and JTree override this method to return a string associated with the mouse event location.
getToolTipLocation(MouseEvent e) : Returns the location (in the receiving component’s coordinate system) where the upper left corner of the component’s tool tip appears.
The following programs will illustrate the use of tooltip text
1. Program to create a textarea and single line tool tip text to it
// java Program to create a textarea and
// single line tool tip text to it
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
class solve extends JFrame {
// frame
static JFrame f;
// text areas
static JTextArea t1;
// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("frame");
// create a object
solve s = new solve();
// create a panel
JPanel p = new JPanel();
// create a text area
t1 = new JTextArea(20, 20);
// set tooltip text
t1.setToolTipText("this is a text Area");
// add text area
p.add(t1);
// add panel
f.add(p);
// set the size of frame
f.setSize(300, 300);
f.show();
}
}
2. Program to create a text area and add multiple line tooltip text to it.
// java Program to create a text area and add
// multiple line tooltip text to it.
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
class solve extends JFrame {
// frame
static JFrame f;
// text areas
static JTextArea t1;
// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("frame");
// create a object
solve s = new solve();
// create a panel
JPanel p = new JPanel();
// create a text area
t1 = new JTextArea(20, 20);
// create a multi line string using html using break tags
String s1 = "<html> this is a text area <br> please add text to it <br> it has 20 rows <br> it has 20 columns </html> ";
// set tooltip text
t1.setToolTipText(s1);
// add text area
p.add(t1);
// add panel
f.add(p)
// set the size of frame
f.setSize(300, 300);
f.show();
}
}
Comments
Post a Comment