3

I need help to deactivate the Mnemonic for JButton. Actually I am using 3rd party API, which they set mnemonic as "Alt C". So I want to remove this mnemonic and wants to set nothing (i.e wants to remove the mnemonic) for this compButton.

    // Alt + 'C' selects the comp.
     compButton.setMnemonic(KeyEvent.VK_C);
mgr
  • 641
  • 1
  • 8
  • 23

1 Answers1

4

How about using the compButton.setMnemonic(0);

edit:

I saw javax/swing/AbstractButton.java:

/**
 * Returns key bindings associated with this object
 *
 * @return the key bindings, if supported, of the object;
 * otherwise, null
 * @see AccessibleKeyBinding
 * @since 1.4
 */
public AccessibleKeyBinding getAccessibleKeyBinding() {
    int mnemonic = AbstractButton.this.getMnemonic();
    if (mnemonic == 0) {
        return null;
    }
    return new ButtonKeyBinding(mnemonic);
}

Therefore, compButton.setMnemonic(0); looks even better than compButton.setMnemonic(-1);.

aterai
  • 9,658
  • 4
  • 35
  • 44
  • 1
    0 is actually defined for KeyEvent. I would suggest using a negative value if you want to take this approach. – user1803551 Mar 25 '14 at 15:30