Java Reference
In-Depth Information
Most of the time, you don't access the ButtonModel directly. Instead, the components that
use the ButtonModel wrap their property calls to update the model's properties.
Note The DefaultButtonModel also lets you get the listeners for a specific type with public
EventListener[ ] getListeners(Class listenerType) .
Understanding AbstractButton Mnemonics
A mnemonic is a special keyboard accelerator that when pressed causes a particular action to
happen. In the case of the JLabel discussed earlier in the “JLabel Class” section, pressing the
displayed mnemonic causes the associated component to get the input focus. In the case of an
AbstractButton , pressing the mnemonic for a button causes its selection.
The actual pressing of the mnemonic requires the pressing of a look-and-feel-specific
hotkey (the key tends to be the Alt key). So, if the mnemonic for a button were the B key, you
would need to press Alt-B to activate the button with the B-key mnemonic. When the button is
activated, registered listeners will be notified of appropriate state changes. For instance, with
the JButton , all ActionListener objects would be notified.
If the mnemonic key is part of the text label for the button, you'll see the character under-
lined. This does depend on the current look and feel and could be displayed differently. In
addition, if the mnemonic isn't part of the text label, there will not be a visual indicator for
selecting the particular mnemonic key, unless the look and feel shows it in the tooltip text.
Figure 4-11 shows two buttons: one with a W-key mnemonic, and the other with an H-key
mnemonic. The left button has a label with W in its contents, so it shows the first W underlined.
The second component doesn't benefit from this behavior on the button, but in the Ocean look
and feel, identifies it only if the tooltip text is set and shown.
Figure 4-11. AbstractButton mnemonics
To assign a mnemonic to an abstract button, you can use either one of the setMnemonic()
methods. One accepts a char argument and the other an int . Personally, I prefer the int variety,
in which the value is one of the many VK_* constants from the KeyEvent class. You can also
specify the mnemonic by position via the displayedMnemonicIndex property.
AbstractButton button1 = new JButton("Warning");
button1.setMnemonic(KeyEvent.VK_W);
content.add(button1);
 
Search WWH ::




Custom Search