The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
< Day Day Up > |
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. (See Figure 90.) You can see this by running the ButtonDemo example, which is explained in How to Use Buttons (page 156) and shown in Figure 90. Figure 90. A tool tip appears when the cursor pauses over any 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 [199] and ToolTipManager . [200] [199] JToolTip API documentation: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JToolTip.html. [200] ToolTipManager API documentation: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/ToolTipManager.html. Table 100 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. Table 100. Tool-Tip API in JComponent
Examples That Use Tool Tips
This table shows some examples that use tool tips and where those examples are described.
|
< Day Day Up > |