Have setMnemonic underline a character other than the first occurrence

java, swing

Solution

Use the source, Luke. Looking at the source code of `setMnemonic()` led me quickly to `AbstractButton.setDisplayedMnemonicIndex()`:

Provides a hint to the look and feel as to which character in the text should be decorated to represent the mnemonic.

Problem

In a re-implementation of an existing program, I would like to keep the message text that users are familiar with. One of the enhancements I'd like to add is a good keyboard-only interface including mnemonics. But using the intuitive mnemonic character with the existing text gives some ugly results. For example: ``` useUpperCheckBox = new JCheckBox("Use UPPERCASE letters"); useUpperCheckBox.setMnemonic(KeyEvent.VK_U); ``` underlines the "U" in "Use" rather than the "U" in "UPPERCASE". Since the user's eye is naturally drawn to "UPPERCASE" looking for a mnemonic, the default location of the decoration is a bit unintuitive. Yes, I've read the docs and the tutorials that say that the first instance of the mnemonic character is underlined, but that isn't what I want. It comes up often enough that I can't believe I'm the only one frustrated by this. Surely someone smarter than me has figured out how to place the decoration somewhere different than the default location.

Original source