Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5142543
  • 博文数量: 1696
  • 博客积分: 10870
  • 博客等级: 上将
  • 技术积分: 18357
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-30 15:16
文章分类
文章存档

2017年(1)

2016年(1)

2015年(1)

2013年(1)

2012年(43)

2011年(17)

2010年(828)

2009年(568)

2008年(185)

2007年(51)

分类: Java

2007-09-29 16:05:08

How to Use Tool Tips
Creating a tool tip for any JComponent object is easy. 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 not react 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 . Here is a picture of the tool tip that appears when the cursor pauses over the left button in the ButtonDemo example.

ButtonDemo showing a tool tip.

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 passed 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. The and sections provide examples of tool tip text determined by a custom renderer. An alternative that works for all JComponents is creating a subclass of the component and overriding its getToolTipText(MouseEvent) method.

Most of the API you need in order to set up tool tips belongs to the JComponent class, and thus is inherited by most Swing components. More tool tip API can be found in individual classes such as JTabbedPane. In general, those APIs are sufficient for specifying and displaying tool tips; you usually do not need to deal directly with the implementing classes and .

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

Method Purpose
If the specified string is not null, then this method registers the component as having a tool tip and, when displayed, gives the tool tip the specified text. If the argument is null, then this method turns off the tool tip for this component.
Returns the string that was previously specified with setToolTipText.
By default, returns the same value returned by getToolTipText(). Multi-part components such as , , and 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.
Returns the location (in the receiving component's coordinate system) where the upper left corner of the component's tool tip appears. 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.

阅读(1807) | 评论(0) | 转发(0) |
0

上一篇:StarUML

下一篇:XEmbed —— 一种桌面技术

给主人留下些什么吧!~~