I l@ve RuBoard |
![]() ![]() |
27.3 TooltipsYou have probably already seen several examples of using basic tooltips with components such as JButton and JLabel. The following classes give you access to much more of the tooltip system in case you need to develop something beyond simple text tips. 27.3.1 The ToolTipManager ClassThis class manages the tooltips for an application. Following the singleton pattern, any given virtual machine will have, at most, one ToolTipManager at any time—a new instance is created when the class is loaded. (Of course, you're already familiar with Design Patterns by Gamma et al., right?) You can retrieve the current manager using the ToolTipManager.sharedInstance( ) method. 27.3.1.1 PropertiesThe ToolTipManager properties shown in Table 27-3 give you control over the delays (in milliseconds) involved in showing tooltips and determines whether or not tooltips are even active. The enabled property determines whether or not tooltips are active. The dismissDelay property determines how long a tooltip remains on the screen if you don't do something to dismiss it manually (such as move the mouse outside the component's borders). The initialDelay property determines how long the mouse must rest inside a component before the tooltip pops up, and reshowDelay determines how long you must wait after leaving a component before the same tooltip shows up again when you reenter the component. (These properties are used by a timer whose delays are triggered by mouse events.) If the lightWeightPopupEnabled property is true, all-Java tooltips are used. A false value indicates native tooltips should be used.
27.3.1.2 Miscellaneous methodsIf need be, you can manually register or unregister components with the manager. Normally this is done using the JComponent.setToolTipText( ) method for the component itself. If you pass in a non-null tip string, the component is registered. If you pass in a null tip string, the component is unregistered. The VSX2 example in Chapter 17 shows off how to use these methods in detail, but the synopsis is: ToolTipManager.sharedInstance( ).registerComponent(yourComponent);
27.3.2 The JToolTip ClassOf course, the whole purpose of having a tooltip manager is to manage tooltips. The tooltips themselves are simple popups containing a short, descriptive string that often shows up if you let your mouse cursor rest over a component. They are embodied here in the JToolTip class. JToolTip is a fairly simple class, thanks to the MVC architecture in place for Swing. All it really needs to know is what text to display and who to display it for. With SDK 1.4, JToolTip can take advantage of the same HTML formatting as the JLabel class. See Chapter 4 for all the details. If you want to display something besides text, you can create your own subclass of JToolTip and render just about anything you want. (Recall that JToolTip extends from JComponent.) To make your custom tooltip available, you'll also need to subclass the component to which you want to add your tip and override the createToolTip( ) method to return an instance of your tip. 27.3.2.1 PropertiesThe properties that support the JToolTip class are shown in Table 27-4. The component property determines which component this tip applies to. The tipText property contains the text to display for the tip. Both of these properties are currently stored in package-private variables.
27.3.2.2 ConstructorThe JToolTip class has only one constructor: ![]() |
I l@ve RuBoard |
![]() ![]() |