Previous | Next | Trail Map | Creating a GUI with JFC/Swing | Using Swing Components

How to Use Tool Tips

Creating a tool tip for any JComponent is easy. You just use the setToolTipText method to set up a tool tip for the component. For example, to add tool tips to three buttons, you add only three lines of code:
b1.setToolTipText("Click this button to disable the middle button.");
b2.setToolTipText("This middle button does nothing when you click it.");
b3.setToolTipText("Click this button to enable the middle button.");
When the user of the program pauses with the cursor over any of the program's buttons, the tool tip for the button comes up. You can see this by running the ButtonDemo example, which is explained in How to Use Buttons, Check Boxes, and Radio Buttons. Here's a picture of the tool tip that appears when the cursor pauses over the left button in ButtonDemo:
[Please imagine a cursor over the left button. Thank you.]

The Tool Tip API

Most of the API you need to set up tool tips is in the JComponent(in the API reference documentation), and thus is inherited by every Swing component (except for top-level containers). This API is covered a table later in this section.

More tool-tip API is in individual classes such as JTabbedPane. Each component's page has information on its tool-tip API, if any.

If you want to bypass or customize the default handling of tool tips, then you'll probably need to deal directly with JToolTip(in the API reference documentation) or ToolTipManager(in the API reference documentation). [PENDING: should I say more?]

Tool Tip API in JComponent
Method Purpose
setToolTipText(String)
(in JComponent)
If the specified string is non-null, then this method registers the component as having a tool tip and makes the tool tip (when displayed) have the specified text. If the argument is null, then this method turns off tool tips for this component.
String getToolTipText()
(in JComponent)
Returns the string that was previously specified with setToolTipText.
String getToolTipText(MouseEvent)
(in JComponent)
By default, 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. For example, each tab in a tabbed pane can have different tool-tip text.
setToolTipLocation(Point)
Point getToolTipLocation()

(in JComponent)
Set or get the location (in the receiving component's coordinate system) where the upper left corner of the component's tool tip will appear. The default value is null, which tells the Swing system to choose a location.

Examples that Use Tool Tips

This table shows the examples that use tool tips and where those examples are described.

Example Where Described Notes
ButtonDemo.java This page and How to Use Buttons, Check Boxes, and Radio Buttons Uses a tool tip to provide instructions for a button.
IconDemoApplet.java How to Use Icons Uses a tool tip to provide name and size information on an image.
TabbedPaneDemo.java How to Use Tabbed Panes Specifies tool tip text for each tab.
TableRenderDemo.java How to Use Tables Adds tool tips using custom renderers and editors.
ActionDemo.java How to Use Actions Adds tool tips to buttons created using Actions.
[PENDING: tree? list? anything else?]


Previous | Next | Trail Map | Creating a GUI with JFC/Swing | Using Swing Components