• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

button mnemonic

 
K. Tsang
Bartender
Posts: 3521
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kevin Florish
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11943
211
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
K. Tsang
Bartender
Posts: 3521
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Andrew. I will keep it simple for the assignment by using Alt+key.
 
Alecsandru Cocarla
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic