5.13. Tool tips
Last updated: 25 January 2013.
This tutorial will show you how to use tool tips in Java.
A tool tip is a description of a component that appears when the user hovers the component. The class JComponent provides a method named setToolTipText(String text) that binds a tool tip to a component. The following picture shows a frame containing a text field with a tool tip describing it:
Here is the code:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public final class ToolTips extends JFrame {
public ToolTips() {
init();
addComponents();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void addComponents() {
// Set up the content pane
JPanel contentPane = new JPanel();
// Set up the text field
JTextField textField = new JTextField(10);
textField.setToolTipText("Enter your name here");
contentPane.add(textField);
// Add the content pane to the JFrame
add(contentPane);
}
private void init() {
setTitle("ToolTips");
setSize(250, 70);
setLocationRelativeTo(null);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ToolTips();
}
});
}
}
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public final class ToolTips extends JFrame {
public ToolTips() {
init();
addComponents();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void addComponents() {
// Set up the content pane
JPanel contentPane = new JPanel();
// Set up the text field
JTextField textField = new JTextField(10);
textField.setToolTipText("Enter your name here");
contentPane.add(textField);
// Add the content pane to the JFrame
add(contentPane);
}
private void init() {
setTitle("ToolTips");
setSize(250, 70);
setLocationRelativeTo(null);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ToolTips();
}
});
}
}
You are here :
JavaPerspective.com >
Intermediate Tutorials >
5. Graphical User Interfaces >
5.13. Tool tips
Next tutorial : JavaPerspective.com > Intermediate Tutorials > 5. Graphical User Interfaces > 5.14. Buttons
Next tutorial : JavaPerspective.com > Intermediate Tutorials > 5. Graphical User Interfaces > 5.14. Buttons