Java Reference
In-Depth Information
Let's see toolbar button inaction in action in Sketcher.
Try It Out - Disabling Actions
We will disable the actions corresponding to the
Save
,
Close
and
Print
actions. Add the following
statements to the end of the
SketchFrame
constructor:
// Disable actions
saveAction.setEnabled(false);
closeAction.setEnabled(false);
printAction.setEnabled(false);
That's all that's necessary. If you run the modified version of Sketcher, menu items and toolbar buttons
corresponding to the
Action
objects we have disabled will be grayed out and non-operational.
If you extend the
File
menu, you will see that the corresponding menu items are grayed out, too.
How It Works
The state of both the
JMenuItem
and
JButton
objects created from an
Action
object is determined by
the state of the
Action
object. Disabling the
Action
object disables any menus or toolbar buttons created
from it. If you want a demonstration that they really are disabled, try disabling a couple of the color 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 will be applying the
techniques from this chapter throughout the rest of the topic.
The most important points we have discussed in this chapter are:
A user interaction generates an event in the context of a component.
❑
There are two categories of events associated with a component:
low-level events
from the
mouse, keyboard or window system events such as opening or closing a window, and
semantic
events
which represent component actions such as pressing a button or selecting a menu item.
❑