Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


ToolTip Support

ToolTips are the pop-up windows common in modern user interfaces that present the user with a short informative message when the mouse rests over the component. ToolTips are also known as flyover help. The JComponent class contains a client property for ToolTips. The ToolTip is stored as a client property rather than a member variable to save space. All JComponent instances do not contain ToolTips, so allocating a reference to a string in every instance is obsessive.

The setToolTipText method can be used to set the ToolTip for a J component. The following is a simple example for adding a ToolTip to a JButton instance:

JButton button = new JButton( “Help” );
button.setToolTipText( “Open the on-line help for this window” );

When the setToolTipText method is called with a non-null string, the component is registered with the ToolTip manager to enable ToolTips. If the string is null, the component is removed from the ToolTip manager. The ToolTip manager and its options are described in Chapter 25, “ToolTips and Debug Graphics.”

The current value of the ToolTip property can be queried with the getToolTipText method. This method returns the current String value of the ToolTip client property or null if a ToolTip hasn’t been registered for the component.

There are many situations when the ToolTip for a component is not static. Instead, it depends on the current state of the component or where the mouse is located in the component. When creating components that extend the JComponent class, the getToolTipText method that requires a MouseEvent as its parameter can be overridden. In this method, the component determines the ToolTip for the current state of the component and for the mouse event. The String ToolTip is returned from this method and used as the ToolTip. The component can also specify where the ToolTip is to be displayed. The JComponent class contains the getToolTipLocation method. The ToolTip manager calls this method before displaying a ToolTip for the component. If the method returns null as the default implementation in JComponent, the ToolTip manager determines the location for the ToolTip. However, this method can be overridden to return a Point that the ToolTip manager will use as the location for the ToolTip.

Border Property

The JComponent class contains a Border property. This property refers to a class that implements the Border interface. When a border has been specified for a component, the component handles painting the border.

Having a Border property in the JComponent class provides an easy mechanism for adding a border to any JComponent instance. Moreover, since a JComponent is a Container, it provides an easy mechanism for adding a border to a group of components. For example, Figure 3.2 shows the results of running the following code:

JPanel panel = new JPanel();
panel.setLayout( new GridLayout( 3, 2 ) );
JLabel label = new JLabel( “First Name” );
panel.add( label );
JTextField firstNameText = new JTextField();
panel.add( firstNameText );
label = new JLabel( “Last Name” );
panel.add( label );
JTextField lastNameText = new JTextField();
panel.add( lastNameText );
label = new JLabel( “Age” );
panel.add( label );
JTextField ageText = new JTextField();
panel.add( ageText );
panel.setBorder( BorderFactory.createTitledBorder( “Age Data” ) );


Figure 3.2  Border around multiple components.

In the figure, a titled border is placed around a JPanel instance that contains labels and text fields. A border is added to a component by using the setBorder method contained in the JComponent class. The Border property is a bound property of a JComponent with the name border. Unfortunately, at the time of this writing, the property names are hard coded in the JComponent class rather than defining constants with the property name. The complete list of bound properties defined in the JComponent class is presented in Table 3.1 later in this chapter. Setting the border will invalidate the component. Similarly, the current border can method. In the example, only one component contains a border; however, any number of components can contain a border to achieve the desired visual effect.

Table 3.1 Bound Properties Defined in the JComponent Class

Property Name Setter Method Getter Method

UI setUI not available
PreferredSize setPreferredSize getPreferredSize
MaximumSize setMaximumSize getMaximumSize
MinimumSize setMinimumSize getMinimumSize
Border setBorder getBorder
Opaque setOpaque isOpaque
Ancestor addNotify getParent
Ancestor removeNotify getParent
property key name putClientProperty getClientProperty


Note:  
The property change support contained in the JComponent class is to migrate to the AWT Component class in a future release of the JDK.

A complete description of creating and using borders is presented in Chapter 5, “Basic Components.”

The Border property has another roll in the JComponent class. When a border has been set for a JComponent instance, the size of the border is used as the insets property for that component. Recall that JComponent extends the AWT Container class, so even JFC classes that aren’t typically used as containers have the insets property. When the getInsets method of the JComponent class is called, it first checks if the border has been set. If so, the border’s Insets are returned to the caller. If not, the Insets from the getInsets method of its parent class, Container, are returned.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.