Tool Tip
Summary:
The Tool Tip is used in conjunction with any Swing component. The Tool Tiip is used to give more in-depth information about a component. An example could be for a JButton. The button may only display an icon, but a user may not know what that icon means. The user would then place the mouse over the button in order to view its Tool Tip. The Tool Tip can be a short sentence that explains the functionality of that button. The Tool Tip can be used to explain functionality or to explain what a component is displaying to the user.
Where to Find It:
- javax.swing.JToolTip
- javax.swing.ToolTipManager
The Basics:
The easiest way to use a Tool Tip is to utilize the JComponent setToolTipText() method. The argument for this method is a String which is the text to display. An example using a JButton would be:
JButton button = new JButton( );
Button.setToolTipText(“The tool tip text”);
This technique can be applied to any JComponent in the Java libraries. Once a JComponent is placed into your application the text of its Tool Tip can be set.
Customization:
A second way to use a
Tool Tip would be using the JToolTip class. An instance of a
JToolTip can be associated with any Java JComponent.
If customization of
JToolTip methods is required then the JToolTip class can be extended to
create the customization.
The follwing
code is an example using a custom Tool Tip.
JButton b = new JButton("Quit") {
// override to use custom Tool Tip the parameter is 'this' which
// refers to the JButtoon b, because this is a inner class associated with that
// JButton b.
public JToolTip createToolTip() { return(new CustomToolTip(this)); }
} ;
b.setToolTipText("Exits the application");
To implement custom
Tool Tips, The above code would
use the extended JToolTip class shown below.
// A custom Tool Tip class that simply changes the
// background color of the basic Tool Tip.
class CustomToolTip extends JToolTip {
// Constructor
public CustomToolTip(JComponent component) {
super();
// sets our button to be associated with this CustomToolTip
setComponent(component);
setBackground(Color.yellow);
}
}The constructor for CustomToolTip takes a single parameter specifying the component with which the Tool Tip is to be associated. The contents of this parameter are passed to the JToolTip.setComponent method. The constructor then sets the background color to yellow. You could also call setFont here to change the font used for custom Tool Tips.
All of the Tool Tips in a system can be managed using the ToolTipManager class. The ToolTipManager allows the programmer to set how long of a delay there will be before Tool Tips are shown. The length of time the Tool Tip will be shown and how long of a delay there will be before a Tool Tip is reshown can also be set. The ToolTipManager mouse events can be overridden if it is necessary to do so. The follwing is an example of code that could be placed into any GUI implementation and it would control the attributes of all of the Tool Tips.
// Show tool tips immediately
// Calling ToolTipManager.sharedInstance() returns a static instance of the ToolTipManager class
// that can be used to set any attributes desired.
ToolTipManager.sharedInstance().setInitialDelay(0);
// Show tool tips after a second
initialDelay = 1000;
ToolTipManager.sharedInstance().setInitialDelay(initialDelay);
Best Practices:
The most common use for
Tool Tips is going to be just setting the text for a component.
When deciding what to put in the text make sure that the
message is
meaningful, but
not too lengthy. The text should be relatively short, but should
convey more information about how the componet works
or what the
component does.
Limitations:
The Tool Tip should not be used when the information that is to be conveyed should be read by all users. If it is instructions for filling out a form, or other information that all users should read, then the Tool Tip should not be used. In those cases a component like a JLabel should be used, because they display their information all the time. The Tool Tip may never be viewed and is used primarily as an aide to users who may be unfamiliar with the interface.
Things to Watch Out for When Including Tool Tips:
The ToolTipManager class will affect all Tool Tips on all of the components in the GUI. If the delay is set to one second, then all Tool Tips will delay for one second.
References:
Other Helpful Resources: