RFR: 4850101: Setting mnemonic to VK_F4 underlines the letter S in a button.

Karl T duke at openjdk.org
Sat Aug 13 14:00:12 UTC 2022


On Tue, 2 Aug 2022 10:22:48 GMT, Prasanta Sadhukhan <psadhukhan at openjdk.org> wrote:

> It is seen that using VK_F4 in JButton's setMnemonic(int i), causes the letter 'S' in the JButton to be underlined if JButton's text has 'S' in its string.
> This is because [**VK_F4**](https://github.com/openjdk/jdk/blob/master/src/java.desktop/share/classes/java/awt/event/KeyEvent.java#L506) keycode is 0x73 (115 in decimal) which happens to be value of lowercase [**'s'** ](https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html) so as per the logic implemented in SwingUtilities.findDisplayedMnemonicIndex(), the first index of the character (either lowecasr or uppercase) is returned as a mnemonic, which gets underlined.
> 
> Fix is made to ignore Action Keys from VK_F1->VK_F11 as displayed mnemonic which corresponds to lowercase p->z

src/java.desktop/share/classes/javax/swing/SwingUtilities.java line 2089:

> 2087:            return -1;
> 2088:         }
> 2089: 

There are some issues with this implementation IMHO:
- why invoke `KeyEvent.getKeyText(mnemonic)` eleven times? This creates eleven temporary strings. The result is always the same.
- `KeyEvent.getKeyText()` may return localized strings, which would break the fix
- why not simply compare `mnemonic` with ` KeyEvent.VK_F*`?

Following should do the same:
~~~java
if (mnemonic >= KeyEvent.VK_F1 && mnemonic <= KeyEvent.VK_F11) {
    return -1;
}
~~~

But IMO **all lowercase ascii characters** should be excluded because there are more `VK_` keys in this range (e.g. `setMnemonic(VK_NUMPAD1)` would underline the 'a' character, but `Alt+A` does not click the button):

~~~java
if (mnemonic >= 'a' && mnemonic <= 'z') {
    return -1;
}
~~~

-------------

PR: https://git.openjdk.org/jdk/pull/9712



More information about the client-libs-dev mailing list