aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes button mnemonic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "button mnemonic" Watch "button mnemonic" New topic
Author

button mnemonic

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2617
    
    9

Hi all, I want to know about the mnemonics we set for buttons. According to the docs, if we say have a JButton and setMnemonic(VK_S) on it - we need to Alt+S to trigger the mnemonic.

Is it possible not to use the Alt key combination? Eg I have a form with 2 buttons - submit and cancel - with multiple text fields. When I hit "Enter" or "Return" it triggers the submit button.

In another words, how to setMnemonic without using Alt key?


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Kevin Florish
Ranch Hand

Joined: Jan 06, 2009
Posts: 176
Hi K (what does the K stand for ? )

Here's what the method says:
setMnemonic

public void setMnemonic(int mnemonic)
Sets the keyboard mnemonic on the current model. The mnemonic is the key which when combined with the look and feel's mouseless modifier (usually Alt) will activate this button if focus is contained somewhere within this button's ancestor window.

A mnemonic must correspond to a single key on the keyboard and should be specified using one of the VK_XXX keycodes defined in java.awt.event.KeyEvent. Mnemonics are case-insensitive, therefore a key event with the corresponding keycode would cause the button to be activated whether or not the Shift modifier was pressed.

If the character defined by the mnemonic is found within the button's label string, the first occurrence of it will be underlined to indicate the mnemonic to the user.


Parameters:
mnemonic - the key code which represents the mnemonic
See Also:
KeyEvent, setDisplayedMnemonicIndex(int)


So I think if you can modify the mouseless modifier in the 'look and feel' it is doable, although using alt for mnemonics is a standard most users are used to.


Java5 Tutorials | Java6 Tutorials | HTML/CSS Tutorials | JavaScript and jQuery Tutorials
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11525
    
100

You might be able to do this by setting an overall key listener for your panel. Normally this would be a bad idea as it would slow down your application, but if this is just a temporary dialog box you might be able to get away with it.



Note that this is just a (poor) proof of concept. I would not want to see any real code go out like this. In particular, I am cheating by setting the key listener to the JPanel, then resetting the focus to the JPanel whenever anything happens. This results in the very poor experience that the user will never see focus where they want it. You would probably be much better off having a common key listener that gets assigned to each field (and button), so no matter where the focus is, the Enter key will still work in a desirable manner.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2617
    
    9

Thanks Andrew. I will keep it simple for the assignment by using Alt+key.
Alecsandru Cocarla
Ranch Hand

Joined: Feb 29, 2008
Posts: 158
Actions (and probably also buttons, but I don't know for sure - you'll have to check the javadoc yourself) allow using an ACCELERATOR_KEY, which is what you want. Also, JRootPane (JFrame.getRootPane()) has a setDefaultButton() method, which you can use for setting a default button (when you press Enter, it's action gets executed).

So, in fact you have 4 ways to "press" a button:

- mnemonic (Alt + key)
- accelerator key (key)
- default button (only one per root pane - Enter)
- when a button has focus (Space)


SCJP 1.4 100%
SCJD 99.5%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: button mnemonic