Java Reference
In-Depth Information
JToolTip Class
The Swing components support the ability to display brief pop-up messages when the cursor
rests over them. The class used to display pop-up messages is JToolTip .
Creating a JToolTip
Calling the public void setToolTipText(String text) method of JComponent automatically
causes the creation of a JToolTip instance when the mouse rests over a component with the
installed pop-up message. You don't normally call the JToolTip constructor directly. There's
only one constructor, and it's of the no-argument variety.
Tooltip text is normally one line long. However, if the text string begins with <html> (in any
case), then the contents can be any HTML 3.2 formatted text. For instance, the following line
causes the pop-up message shown in Figure 4-3:
component.setToolTipText("<html>Tooltip<br>Message");
Figure 4-3. HTML-based tooltip text
Creating Customized JToolTip Objects
You can easily customize the display characteristics for all pop-up messages by setting UIResource
elements for JToolTip , as shown in the “Customizing a JToolTip Look and Feel” section later in
this chapter.
The JComponent class defines an easy way for you to customize the display characteristics
of the tooltip when it's placed over a specific component. Simply subclass the component you
want to customize and override its inherited public JToolTip createToolTip() method. The
createToolTip() method is called when the ToolTipManager has determined that it's time to
display the pop-up message.
To customize the pop-up tooltip appearance, just override the method and customize the
JToolTip returned from the inherited method. For instance, the following source demonstrates
the setting of a custom coloration for the tooltip for a JButton , as shown in Figure 4-4.
JButton b = new JButton("Hello, World") {
public JToolTip createToolTip() {
JToolTip tip = super.createToolTip();
tip.setBackground(Color.YELLOW);
tip.setForeground(Color.RED);
return tip;
}
};
 
Search WWH ::




Custom Search