I have added ENTER Key to the default FocusTraversalKeys as such...
private void focus() {
Set forwardKeys = getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
Set newForwardKeys = new java.util.HashSet(forwardKeys);
newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, newForwardKeys);
}
I have a Product Information form that I am going through using the focus but when it comes to a save button I would like to CLICK the button instead of the focus going to the next component.
I have added a KeyPressed and KeyReleased listener to the button and then tried this...
private void saveButtonKeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
evt.consume();
saveButton.doClick();
}
}
This same method works on my Text Area BUT the code doesn't do the CLICK instead puts the focus on next component which is also a button.
Please suggest something that would help me achieve the required result. Find below the Image of the form used.