Java Reference
In-Depth Information
When the button is added to a container and displayed, it appears normally.
When the user rolls the mouse pointer over the button, hovering there momen-
tarily, the tool tip text pops up. When the user moves the mouse pointer off of the
button, the tool tip text disappears.
A mnemonic is a character that allows the user to push a button or make a
menu choice using the keyboard in addition to the mouse. For example, when a
mnemonic has been defined for a button, the user can hold down the ALT key
and press the mnemonic character to activate the button. Using a mnemonic to
activate the button causes the system to behave just as it would if the user had
used the mouse to press the button.
A mnemonic character should be chosen from the label on a button or menu
item. Once the mnemonic has been established using the setMnemonic method, the
character in the label will be underlined to indicate that it can be used as a shortcut.
If a letter is chosen that is not in the label, nothing will be underlined and the user
won't know how to use the shortcut. You can set a mnemonic as follows:
JButton button = new JButton ("Calculate");
button.setMnemonic ('C');
When the button is displayed, the letter C in Calculate is underlined on the but-
ton label. When the user presses ALT-C, the button is activated as if the user had
pressed it with the mouse.
Some components can be
disabled if they should not be used. A disabled com-
ponent will appear “grayed out,” and nothing will happen if the user attempts to
interact with it. To disable and enable components, we invoke the setEnabled
method of the component, passing it a boolean value to indicate whether the
component should be disabled (false) or enabled (true). For example:
JButton button = new JButton ("Do It");
button.setEnabled ( false );
Disabling components is a good idea when users should not be allowed to use
the functionality of a component. The grayed appearance of the disabled com-
ponent is an indication that using the component is inappropriate (and, in fact,
impossible) at the current time. Disabled components not only convey to the user
which actions are appropriate and which aren't, they also prevent erroneous situ-
ations from occurring.
Let's look at an example that uses tool tips, mnemon-
ics, and disabled components. The program in Listing 11.8
presents the image of a light bulb and provides a button to
turn the light bulb on and a button to turn the light bulb off.
KEY CONCEPT
Components should be disabled
when their use is inappropriate.
 
Search WWH ::




Custom Search