Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Swing has its own set of difficult tasks, but setting the basic "tooltip" help text on Java components (JLabel, JTextField, JComboBox, etc.) can actually be quite simple. You only need to use the setToolTipText method to set the "tooltip" text on the desired components for very basic rollover help. All Java component sub-classes inherit this tooltip method from the JComponent class.
This Java tooltip tutorial first looks at a simple tooltip example and then creates a Java class showing how tooltips can be used across a variety of Java/Swing components.
How to use ToolTip in Java Swing
A tool tip can be created for any JComponent by using the setToolTipText() method. It can be used to create tooltips for components.
It takes just one line of code to add tooltips to PasswordField, for example:
field.setToolTipText("Enter your password");
Methods used in ToolTip
By using the following method setToolTipText(String s), we can add tooltip text to almost all Java Swing components. By calling this method, the tooltip of a component is set to a specified string. As soon as the cursor reaches the boundary of that component, a pop-up appears with text.
Methods used:
getToolTipText() : A component's tooltip text is returned.
setToolTipText(String s) : This function sets the component's tooltip text.
getToolTipText(MouseEvent e): This method returns the same value as getToolTipText(). Components such as JTabbedPane, JTable, and JTree override this method to return a string indicating the mouse event's location.
getToolTipLocation(MouseEvent e) : This method returns the coordinates of the upper left corner of the component's tooltip (in the receiving component's coordinate system).
A simple Java/Swing tooltip example
As an example of how to set a tooltip or balloon help on a JTextField, here is a snippet of Java code:
JTextField textfield = new JTextField(10);
textfield.setToolTipText("Enter your username, up to ten characters in length, letter and numbers only.");
This is how the text looks when I roll over this JTextField.
A tooltip can easily be added to a JTextField as you can see. Java buttons and other components can also be easily enhanced with tooltips, and I'll demonstrate this in a complete Java class later.
A complete Java tooltip example
In addition to the short tooltip example, I'd like to share this complete Java class that illustrates how we can use it with a variety of Java components. The following is the source code for a Java class that constructs a JFrame and adds tooltip help text to a JLabel, JTextField, and JButton:
package com.hubberspot.example;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ToolTipDemo {
public static void main(String[] args) {
// 1. Create a simple frame by creating an object
// of JFrame.
JFrame frame = new JFrame();
// 2. Give the frame a title "Tool Tip Frame" by calling
// setTitle method on frame object
frame.setTitle("Tool Tip Frame");
// 3. Give frame a size in pixels as say 300,300
frame.setSize(300, 300);
// 4. set a operation when a user close the frame, here it
// closes the frame and exits the application
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 5. Create a button by creating an Object of class
// JButton.
JButton button = new JButton();
// 6. set the text of button as "Button"
button.setText("Button");
// 7. Create a label by creating an Object of class
// JLabel
JLabel label = new JLabel();
// 8. set the text of label as "Label"
label.setText("Label");
// 9. In order to create a tool tip for label,
// button or any swing components, we call setToolTipText
// method and passing it the information we want to display
// when user hovers over the swing components
button.setToolTipText("Tool Tip for Button");
label.setToolTipText("Tool Tip for Label");
// 10. for adding all these swing components to frame
// we first get the content pane which returns a
// Container
Container container = frame.getContentPane();
// 11. We create a Layout for Swing Components
// here we are using FlowLayout with value as "center'
// it will make the swing components float to center
FlowLayout layout = new FlowLayout(FlowLayout.CENTER);
// 12. We set the layout for container
container.setLayout(layout);
// 13. We add the button and label to it
container.add(button);
container.add(label);
// 14. after adding button and label, we make it
// visible on the frame by calling the method as
// setVisible and passing value as true.
frame.setVisible(true);
}
}
In order to set tooltip text, which method is used?
JComponent objects can have tooltips created easily. Set a component's tooltip with the setToolTipText method.
How are tooltips added?
In D3.js, tooltips can be created in two ways. The first method is to create the SVG <title> tags as descendants of an interactive element. Second, you can use mouseover, mouseleave, and mousemove events to dynamically move a tooltip and change its visibility.
How do I make my tooltip always visible?
An element's showTooltipOn property displays its tooltip permanently. If you want the tooltip to always appear, set it to "always".
How long can tooltips be?
In addition, you should keep your text short. Less than 150 characters in tooltips (and no more than two lines of body text) make them easier to read.
There are how many types of tooltips?
Tooltips can be divided into two types: Plain Tooltips and Rich Tooltips.
Conclusion
In this blog, we have discussed about ToolTip in Java Swing and have gone through some example code to see how ToolTip works.
Check out this link if you want to read more about Java Frameworks.