Java Reference
In-Depth Information
How It Works
Action
objects act as a repository for the tooltip text for components that implement the actions, so this
works for the toolbar buttons in Sketcher. If an
Action
object contains a tooltip property, a toolbar button
that you create from it automatically has the tooltip operational. Try lingering the cursor over a menu
item. Because the menu items are created from
Action
items, tooltips are available for them, too.
DISABLING ACTIONS
You don't want to have all of the menu items and toolbar buttons in Sketcher enabled all of the time. For
example, 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
setEn-
abled()
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.
You use the ability to disable actions in Sketcher when you implement more operational details. In the
meantime you can see how disabled toolbar buttons and menu items look by adding statements temporarily
to the
SketcherFrame
constructor to disable some actions.
SUMMARY
In this chapter you have learned how to handle events in your applications and in your applets. Events are
fundamental to all window-based applications, as well as most applets, so you can apply the techniques you
have seen in this chapter throughout the rest of the topic.
You also now know how to create menu items and toolbar buttons from
Action
objects. This is a very
powerful technique that simplifies the management of events for these GUI components. You now have a
version of Sketcher that is an application of around 400 lines of code. It doesn't do a lot yet, though. You en-
hance the function of Sketcher and increase the number of lines of code a great deal in subsequent chapters.