• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Set mnemonic key

 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends/sir/madam

I want to set mnemonic key for JPanel which is having a titled border. Can anyone please suggest me how this can be done ?
Example I need to see an underscore on the character 'C' in the String "Calculate" which is used for titled border.

I dont know the exact use as to why this is essential but this is one of the requirement.

Can anyone please help me as to how can I do this ?

Waiting for your suggestions

Thanks and Regards
Rohit.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I need to see an underscore on the character 'C' in the String "Calculate"
> which is used for titled border.

Unfortunately, the String title doesn't recognise html, otherwise you could just have
panel.setBorder(BorderFactory.createTitledBorder("<html><u>C</u>alculate</html>"));

so it seems you need to add a component which displays text that does recognise html eg JLabel

this link has an example of a JCheckBox in the title
http://www.objects.com.au/java/examples/swing/CheckBoxBorder.do

I've butchered the code, to change it to a JLabel, and the remainder seems a bit of overkill,
but at least it displays 'Calculate' with the 'C' underlined.

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually, you should be able to change the component from a JLabel to a
component where you can set the Mnemonic.
 
mark koorevaar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
}

}
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mark koorevaar:
For what it's worth (posts are old but maybe it will help others with the same current problem).

Old is quite an understatement here. Couldn't you have waited another month? I can't remember ever seeing a 3 year kick
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic