33

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 :)

Geesh_SO
  • 2,156
  • 5
  • 31
  • 58
  • The Javadoc is a good source for such questions: http://docs.oracle.com/javafx/2/api/javafx/scene/control/TextField.html – Puce Dec 14 '12 at 16:28
  • 2
    Aaah Stack Overflow, as the saying goes: "Give a person a fish and they eats for a day, but teach them to fish and and they'll eat for a lifetime." But a common SO response is more along the lines of: "I say, good sir, there is a place with books, look up fishing, study and practice hard, and 2 months from now you can eat." Well intentioned, but useless. – Malcolm Anderson Apr 10 '21 at 22:48

6 Answers6

81

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 package and do something like this:

field.setOnKeyPressed(new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent ke) {
        if (ke.getCode().equals(KeyCode.ENTER)) {
            doSomething();
        }
    }
});

Using lambda:

field.setOnKeyPressed( event -> {
  if( event.getCode() == KeyCode.ENTER ) {
    doSomething();
  }
} );
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Brendan
  • 3,589
  • 3
  • 20
  • 11
42

You can use the onAction attribute of the TextField and bind it to a method in your controller.

@FXML
public void onEnter(ActionEvent ae){
   System.out.println("test") ;
}

And in your FXML file:

<TextField fx:id="textfield" layoutX="29.0" layoutY="298.0" onAction="#onEnter" prefWidth="121.0" />
Adowrath
  • 701
  • 11
  • 24
Wottensprels
  • 3,307
  • 2
  • 29
  • 38
  • 3
    yes, the onAction method seems to be the way to go: http://docs.oracle.com/javafx/2/api/javafx/scene/control/TextField.html#onActionProperty -> +1 But you need to annotate the onEnter() method with @FXML when "binding" from FXML and it has to take an ActionEvent argument, AFAIK. – Puce Dec 14 '12 at 16:25
  • Note that I had to add `(ActionEvent actionEvent)` as a parameter on `onEnter` before it would work. – Erhannis Jun 23 '16 at 19:25
9

You can try the following:

@FXML
public void buttonPressed(KeyEvent e)
{
    if(e.getCode().toString().equals("ENTER"))
    {
        //do something
    }
}
Ishrak
  • 509
  • 1
  • 9
  • 17
3

Simply using "lambda expression" :

TextField textField = new TextField();
textField.setOnAction(e -> {
    // add your code to be run here
    System.out.println("textFile");
    });
mhmdar
  • 39
  • 2
2

This works:

@FXML public TextField txt;

@FXML
public void textAction(KeyEvent e){

    if(e.getCode().equals(KeyCode.ENTER))
        System.out.println(txt.getText());
}

If for some reason .getCode() is not working, make sure you import the library:

import javafx.scene.input.KeyEvent;

NOT

import java.awt.event.KeyEvent;

I got caught up on this and it was irritating. Im just passing this on for all those in the same boat.

Nevets17
  • 109
  • 1
  • 9
0

On some keyboards you have to put additional tests to the '\n' and '\r' characters.

if(event.getCode().equals(KeyCode.ENTER) || event.getCharacter().getBytes()[0] == '\n' || event.getCharacter().getBytes()[0] == '\r') {
        // your action
    }