I have the following code of JButton in Java:
enterButton = new JButton("Enter");
enterButton.setMnemonic(KeyEvent.VK_ENTER); // Shortcut: Alt + Enter
The question is simply: Instead of having the shortcut "Alt+Enter", how do I set a shortcut "Enter"?
I simply prefer pressing "Enter" instead of holding "Alt" and pressing "Enter"
I had the same issue: I have a form an whenever I am edditing a field I want to press enter for it to fire the actionPerformed event.
I fixed it with this:
The JPanel
the button and the rest of the form is located on is called content
:
content.getRootPane().setDefaultButton(enterButton);
This keeps the button always selected so when you press enter, its corresponding actionPerformed event fires (remember to add an ActionListener to it!)
Hope this helps you!
Kind regards,
Héctor