I have a TextField to enter a search term, and a button for "Go". But in JavaFX2, how would I make it so pressing the Enter Key in the TextField would perform an action?
Thanks :)
I have a TextField to enter a search term, and a button for "Go". But in JavaFX2, how would I make it so pressing the Enter Key in the TextField would perform an action?
Thanks :)
You can use the onAction attribute of the TextField and bind it to a method in your controller.
And in your FXML file:
Simply using "lambda expression" :
On some keyboards you have to put additional tests to the '\n' and '\r' characters.
This works:
If for some reason .getCode() is not working, make sure you import the library:
NOT
I got caught up on this and it was irritating. Im just passing this on for all those in the same boat.
I'm assuming you want this to happen when the user presses enter only while the TextField has focus. You'll want use KeyEvent out of javafx.scene.input and do something like this...
Hope this is helpful!
You can try the following: