Download the JDK
Search the Tutorials
Hide the TOC
Trail: Creating a GUI with JFC/Swing
Lesson: Using Swing Components
Section: How to Use Various Components
Using Swing Components
A Visual Index to the Swing Components
Using HTML in Swing Components
Using Top-Level Containers
Using Models
The JComponent Class
Using Text Components
Text Component Features
The Text Component API
How to Use Various Components
How to Make Applets
How to Use Buttons, Check Boxes, and Radio Buttons
How to Use Color Choosers
How to Use Combo Boxes
How to Make Dialogs
How to Use Editor Panes and Text Panes
How to Use File Choosers
How to Use Formatted Text Fields
How to Make Frames (Main Windows)
How to Use Internal Frames
How to Use Labels
How to Use Layered Panes
How to Use Lists
How to Use Menus
How to Use Panels
How to Use Password Fields
How to Use Progress Bars
How to Use Root Panes
How to Use Scroll Panes
How to Use Separators
How to Use Sliders
How to Use Spinners
How to Use Split Panes
How to Use Tabbed Panes
How to Use Tables
How to Use Text Areas
How to Use Text Fields
How to Use Tool Bars
How to Use Tool Tips
How to Use Trees
How to Use Icons
How to Use Borders
Solving Common Component Problems
Home Page > Creating a GUI with JFC/Swing > Using Swing Components
« Previous • Trail • Next »
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`:

> For components such as tabbed panes that have multiple parts, it often makes sense to vary the tool-tip text to reflect the part of the component under the cursor. For example, a tabbed pane might use this feature to explain what will happen when you click the tab under the cursor. When you implement a tabbed pane, you can specify the tab-specific tool-tip text in an argument to the `addTab` or `setToolTipTextAt` method.

> Even in components that have no API for setting part-specific tool-tip text, you can generally do the job yourself. If the component supports renderers, then you can set the tool tip text on a custom renderer. You can find examples of doing this in the table and tree sections. An alternative that works for all `JComponent`s is creating a subclass of the component and overriding its `getToolTipText(MouseEvent)` method.

### The Tool Tip API

> Most of the API you need to set up tool tips is in the `JComponent` class, and thus is inherited by most Swing components. More tool-tip API is in individual classes such as `JTabbedPane`. In general, those APIs are sufficient for specifying and displaying tool tips; you usually don't need to deal directly with the implementing classes, `JToolTip` and `ToolTipManager`.

> The following table lists the `JComponent` tool tip API. For information on individual components' support for tool tips, see the how-to section for the component in question.

> Tool Tip API in `JComponent`
> Method | Purpose
> --- | ---
> setToolTipText(String) | 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() | Returns the string that was previously specified with `setToolTipText`.
> String getToolTipText(MouseEvent) | 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.
> Point getToolTipLocation(MouseEvent) | Get the location (in the receiving component's coordinate system) where the upper left corner of the component's tool tip will appear. The argument is the event that caused the tool tip to be shown. The default return value is null, which tells the Swing system to choose a location.

### Examples that Use Tool Tips

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

> Example | Where Described | Notes
> --- | --- | ---
> `ButtonDemo` | This section and How to Use Buttons, Check Boxes, and Radio Buttons | Uses a tool tip to provide instructions for a button.
> `IconDemoApplet` | How to Use Icons | Uses a tool tip in a label to provide name and size information for an image.
> `TabbedPaneDemo` | How to Use Tabbed Panes | Uses an argument to the `addTab` method to specify tool tip text for each tab.
> `TableRenderDemo` | Specifying Tool Tips for Cells | Adds tool tips to a table using a renderer.
> `TableToolTipsDemo` | Specifying Tool Tips for Cells, Specifying Tool Tips for Column Headers | Adds tool tips to a table using various techniques.
> `TreeIconDemo2` | Customizing a Tree's Display | Adds tool tips to a tree using a custom renderer.
> `ActionDemo` | How to Use Actions | Adds tool tips to buttons created using `Action`s.

« Previous • Trail • Next »
Previous page: How to Use Tool Bars
Next page: How to Use Trees