Set KeyPressed event for a TextField in JavaFX
Asked Answered
M

1

5

I have number of TextField objects inside a pop up window called dialog (type : Stage).

I am trying to define an action handler for them which aims to close the stage once the escape button is clicked on the key board.

Here is my function for the stage closer :

public void escapeKeyPressed(KeyCode keyCode , Stage dialog){
    if (keyCode == KeyCode.ESCAPE ){
        dialog.close();
        System.out.println("escape got called");
    }
} 

and the following is where I call it :

textUsername.setOnAction((event) -> {escapeKeyPressed(KeyCode.ESCAPE ,dialog );});
textAddress.setOnAction((event) -> {escapeKeyPressed(KeyCode.ESCAPE ,dialog );});
textwp.setOnAction((event) -> {escapeKeyPressed(KeyCode.ESCAPE ,dialog );});
textState.setOnAction((event) -> {escapeKeyPressed(KeyCode.ESCAPE ,dialog );});
textloginName.setOnAction((event) -> {escapeKeyPressed(KeyCode.ESCAPE ,dialog );});

The problem is that the function doesn't get called.

Any idea how can I fix that? It's woth mentioning that the function by itself works fine if I replace the caller out of setOnAction();

Mindy answered 5/9, 2016 at 10:13 Comment(1)
If you have a Dialog you should consider using the Alert (javafx.scene.control.Alert) class, with ButtonType.CANCEL. This way you'll get a correctly labeled button in the correct order of the OS the application is running on AND Esc will (on platforms where this is considered to be the default behaviour) close the dialog.Swordtail
P
7

The setOnAction documentation for TextField states:

The action handler associated with this text field, or null if no action handler is assigned. The action handler is normally called when the user types the ENTER key.

Therefore, escapeKeyPressed will be executed on Enter key press. What will happen now: if you press Enter key, it will call this method with KeyCode.ESCAPE, therefore it will close the dialog.

Use setOnKeyPressed rather than setOnAction:

Defines a function to be called when this Node or its child Node has input focus and a key has been pressed.

Rather than pass KeyCode.ESCAPE, pass the KeyCode of the KeyEvent what you can get with getCode():

textUsername.setOnKeyPressed(event -> escapeKeyPressed(event.getCode(), dialog));
Photic answered 5/9, 2016 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.