For what it's worth (posts are old but maybe it will help others with the same current problem).
My TitledMnemonicBorder accepts the title, focus component and mnemonic. The mnemonic will be displayed and when invoked the registered action will be performed which will result in requesting the focus for the focusableComponent.
public class TitledMnemonicBorder extends TitledBorder {
private char mnemonic;
private JComponent focusableComponent;
/**
* Constructor.
*
* @param string The title
* @param mnemonic The mnemonic to be shown
* @param focusableComponent The component that will receive the focus when the
* mnemonic is invoked
*/
public TitledMnemonicBorder(String string, char mnemonic, JComponent focusableComponent) {
super(string);
this.mnemonic = mnemonic;
this.focusableComponent = focusableComponent;
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
super.paintBorder(c, g, x, y, width, height);
.. (see TitledBorder code)
// registers the mnemonic and its action to the component
jc.registerKeyboardAction(new ActionListener() {
public void actionPerformed(ActionEvent e) {
focusableComponent.requestFocus();
}
}, KeyStroke.getKeyStroke("alt "+ Character.toUpperCase(mnemonic)),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
.. (see TitledBorder code)
SwingUtilities2.drawStringUnderlineCharAt(jc, g, getTitle(),
getTitle().indexOf(mnemonic), textLoc.x, textLoc.y);
.. (see TitledBorder code)
}
}