Feb 18

Setting the mnemonic for a JButton assigns a hot key for your button, allowing it to be ‘pressed’ via the keyboard using Alt-<Hot Key>

Additionally if the hot key character appears in the button text then the character is also underlined in the button to signify that a hot key is available (when Alt is pressed).

For example to assign Alt-B to a button would be achieved using:

JButton button = new JButton("Objects");

For keys that do not map to a single character (eg. function keys) then the key code can be used. In this case nothing is able to be underlined in the button.

For example to assign Alt-F5 as a hot key for a button use:

JButton button = new JButton("Objects");

written by objects \\ tags: , , ,