Java Reference
In-Depth Information
It works exactly as expected. If there is a value for the SMALL_ICON key in an action, the image is dis-
played on the menu item that you create from it.
It could be that you create a menu item from an Action object that has a value for the SMALL_ICON key,
but you don't want the icon displayed on the menu item. You can prevent the icon from being shown on
a menu item like this:
menuItem.setIcon(null);
The JMenuItem class has a setIcon() method that accepts a reference of type Icon to set an icon for a
menu item. If you want to remove the icon for a menu item, you just call its setIcon() method with the
argument as null . You could also use the method to change the icon.
ADDING TOOLTIPS
I'm sure you have seen tooltips in operation. These are the little text prompts that appear automatically when
you let the mouse cursor linger over certain GUI elements on the screen for a second or two. They disappear
automatically when you move the cursor. I think you will be surprised at how easy it is to implement support
for tooltips in Java.
The secret is again in the Action objects that you are using. Action objects have a built-in capability to
store tooltip text because it is already provided for with the SHORT_DESCRIPTION key. All you have to do is
store the tooltip text as the value for the key.
TRY IT OUT: Implementing Tooltips
You can provide for tooltip text in each of the inner classes by adding a little more code to the cre-
ateFileMenuActions() method in SketcherFrame :
private void createFileMenuActions() {
// Code to create action objects as before...
// Code to add large icons to the action objects as before...
// Code to add small icons to the action objects as before...
// Add tooltip text
newAction.putValue(SHORT_DESCRIPTION, "Create a new sketch");
openAction.putValue(SHORT_DESCRIPTION, "Read a sketch from a
file");
saveAction.putValue(SHORT_DESCRIPTION, "Save the current sketch
to file");
saveAsAction.putValue(SHORT_DESCRIPTION, "Save the current sketch
to a new file");
printAction.putValue(SHORT_DESCRIPTION, "Print the current
sketch");
Search WWH ::




Custom Search