The hammer - of setting the enter as focus traversal key for all component except those which register their own - is just fine if it's really required. The obvious drawback is that default bindings to the enter stop working, in particular
- action/Listeners on textFields
- default buttons
- any other component type with a custom binding to enter
If those side-effects are problematic, there's the less intrusive alternative of tweaking the binding in the shared ancestor actionMap of the textFields.
// "early" in the app instantiate a textField
JTextField text = new JTextField();
ActionMap map = text.getActionMap();
// get a reference to the default binding
final Action notify = map.get(JTextField.notifyAction);
while (map.getParent() != null) {
// walk up the parent chain to reach the top-most shared ancestor
map = map.getParent();
}
// custom notify action
TextAction tab = new TextAction(JTextField.notifyAction) {
@Override
public void actionPerformed(ActionEvent e) {
// delegate to default if enabled
if (notify.isEnabled()) {
notify.actionPerformed(e);
}
// trigger a focus transfer
getTextComponent(e).transferFocus();
}
};
// replace default with augmented custom action
map.put(JTextField.notifyAction, tab);
After replacing the default, all textFields will use the custom action. The one beware is that the replacement has to be repeated whenever the LAF is changed.