File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Set mnemonic key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Set mnemonic key" Watch "Set mnemonic key" New topic
Author

Set mnemonic key

Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Jun 09, 2003
Posts: 4632
actually, you should be able to change the component from a JLabel to a
component where you can set the Mnemonic.
mark koorevaar
Greenhorn

Joined: Jul 31, 2004
Posts: 2
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Set mnemonic key