Java Reference
In-Depth Information
if(tooltip != null) // If there is a tooltip
putValue(SHORT
_
DESCRIPTION, tooltip); // ...squirrel it away
}
The corresponding changes in the
SketchFrame
constructor are:
JMenu colorMenu = new JMenu("Color"); // Color sub-menu
elementMenu.add(colorMenu); // Add the sub-menu
addMenuItem(colorMenu, redAction = new ColorAction
("Red", Color.RED, "Draw in red"));
addMenuItem(colorMenu, yellowAction = new ColorAction
("Yellow", Color.YELLOW, "Draw in yellow"));
addMenuItem(colorMenu, greenAction = new ColorAction
("Green", Color.GREEN, "Draw in green"));
addMenuItem(colorMenu, blueAction = new ColorAction
("Blue", Color.BLUE, "Draw in blue"));
Of course, if you want to put your own tooltip text for any of these, you can. You should keep it short
since it is displayed on the fly. We can try our tooltips out now we have the last piece in place. Just
recompile the
SketchFrame
class and run Sketcher again. You should be able to see the tooltip when
you let the cursor linger over a button.
How It Works
Action
objects act as a repository for the tooltip text for a toolbar button. If an
Action
object
contains a tooltip property, a toolbar button that you create from it will automatically have the tooltip
operational. Try lingering the cursor over a menu item. Since the menu items are also created from
Action
items, tooltips are available for them, too.
Disabling Actions
You won't want to have all of the menu items and toolbar buttons enabled all of the time. For instance,
while there is no sketch active, the
Save
and
Print
menu items should not be operational, and neither
should the corresponding buttons. The
Action
objects provide a single point of control for enabling or
disabling menu items and the corresponding toolbar buttons. To disable an action, you call the
setEnabled()
method for the
Action
object with an argument of
false
. You can restore the
enabled state by calling the method with a
true
argument. The
isEnabled()
method for an
Action
object returns
true
if the action is enabled, and
false
otherwise.