Other answers (including accepted) are good, but if you are already using Java8, you can do the following (in short, newer):
textField.addActionListener( ae -> {
As the accepted answer said, you can simply respond with an ActionListener that catches Enter-Key.
However, my approach uses functional concepts that were introduced in Java 8.
If you want to use the same action, for example, for a button and JTextField, you can do the following:
ActionListener l = ae -> { //do stuff } button.addActionListener(l); textField.addActionListener(l);
If you require further information, please let me know!
Thomas Böhm Aug 21 '17 at 21:16 2017-08-21 21:16
source share