Java Reference
In-Depth Information
This just calls the constructor that accepts arguments defining the name and the keystroke. It then stores
the tooltip string using the
SHORT
_
DESCRIPTION
key, as long as it isn't
null
. Although you wouldn't
expect a
null
to be passed for the tooltip text reference, it's best not to assume it as this could crash the
program. If it is
null
we do nothing.
The other constructor will take care of a tooltip for an
Action
item without an accelerator keystroke:
FileAction(String name, String tooltip) {
this(name); // Call the other constructor
if(tooltip != null) // If there is tooltip text
putValue(SHORT
_
DESCRIPTION, tooltip); // ...squirrel it away
}
Of course, we must now change the code in the
SketchFrame
constructor that creates
FileAction
items so that we incorporate the tooltip argument:
// Create the action items for the file menu
newAction = new FileAction("New", KeyStroke.getKeyStroke('N',Event.CTRL
_
MASK ),
"Create new sketch");
openAction = new FileAction("Open", KeyStroke.getKeyStroke('O',Event.CTRL
_
MASK),
"Open existing sketch");
closeAction = new FileAction("Close", "Close sketch");
saveAction = new FileAction("Save", KeyStroke.getKeyStroke('S',Event.CTRL
_
MASK),
"Save sketch");
saveAsAction = new FileAction("Save As...", "Save as new file");
printAction = new FileAction("Print", KeyStroke.getKeyStroke('P',Event.CTRL
_
MASK),
"Print sketch");
We can do exactly the same with the
TypeAction
class - just add the following constructor definition:
TypeAction(String name, int typeID, String tooltip) {
this(name, typeID);
if(tooltip != null) // If there is a tooltip
putValue(SHORT
_
DESCRIPTION, tooltip); // ...squirrel it away
}
We must then modify the code in the
SketchFrame
constructor to pass a tooltip string when we create
a
TypeAction
object:
// Construct the Element pull down menu
addMenuItem(elementMenu, lineAction = new TypeAction("Line", LINE, "Draw lines"));
addMenuItem(elementMenu, rectangleAction = new TypeAction("Rectangle",RECTANGLE,
"Draw rectangles"));
addMenuItem(elementMenu, circleAction = new TypeAction("Circle", CIRCLE,
"Draw circles"));
addMenuItem(elementMenu, curveAction = new TypeAction("Curve", CURVE,
"Draw curves"));
And a constructor that does exactly the same needs to be added to the
ColorAction
class:
public ColorAction(String name, Color color, String tooltip) {
this(name, color);