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");
button.setMnemonic('B');

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");
button.setMnemonic(KeyEvent.VK_F5);

written by objects \\ tags: , , ,


Leave a Reply

You must be logged in to post a comment.