Details
-
Bug
-
Resolution: Fixed
-
P3
-
jfx11, 9, 10
Description
A DESCRIPTION OF THE PROBLEM :
When registering an onKeyPress() event handler on a TextField, we are unable to consume the event. Specifically, when consuming the [ENTER] key, the default button of the scene is triggered and the event is not consumed by the TextField.
REGRESSION : Last worked in version 8u162
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Enter text into the TextField and press the [ENTER] key.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The TextField's onKeyPressed() method should be called, printing "-> Enter" to the console and the KeyEvent should be consumed.
ACTUAL -
The onAction() method for the scene's default button, btnSave, is called first, printing "-> Save" to the console before triggering the onKeyPressed() method for the TextField.
---------- BEGIN SOURCE ----------
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Simple UI
VBox root = new VBox(10);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);
// TextField
TextField textField = new TextField();
// Capture the KeyCode.ENTER key
textField.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER) {
System.out.println("-> Enter");
event.consume();
}
});
// Buttons
Button btnCancel = new Button("Cancel");
btnCancel.setCancelButton(true);
btnCancel.setOnAction(e -> {
System.out.println("-> Cancel");
primaryStage.close();
});
Button btnSave = new Button("Save");
btnSave.setDefaultButton(true);
btnSave.setOnAction(e -> {
System.out.println("-> Save");
primaryStage.close();
});
ButtonBar buttonBar = new ButtonBar();
buttonBar.getButtons().addAll(btnCancel, btnSave);
root.getChildren().addAll(textField, buttonBar);
primaryStage.setScene(new Scene(root));
primaryStage.setTitle("Consume Test");
primaryStage.show();
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Currently adding a catch to the default button to determine if the TextField has focus:
btnSave.setOnAction(e -> {
if (textField.isFocused()) return;
System.out.println("-> Save");
primaryStage.close();
});
FREQUENCY : always
When registering an onKeyPress() event handler on a TextField, we are unable to consume the event. Specifically, when consuming the [ENTER] key, the default button of the scene is triggered and the event is not consumed by the TextField.
REGRESSION : Last worked in version 8u162
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Enter text into the TextField and press the [ENTER] key.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The TextField's onKeyPressed() method should be called, printing "-> Enter" to the console and the KeyEvent should be consumed.
ACTUAL -
The onAction() method for the scene's default button, btnSave, is called first, printing "-> Save" to the console before triggering the onKeyPressed() method for the TextField.
---------- BEGIN SOURCE ----------
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Simple UI
VBox root = new VBox(10);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);
// TextField
TextField textField = new TextField();
// Capture the KeyCode.ENTER key
textField.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER) {
System.out.println("-> Enter");
event.consume();
}
});
// Buttons
Button btnCancel = new Button("Cancel");
btnCancel.setCancelButton(true);
btnCancel.setOnAction(e -> {
System.out.println("-> Cancel");
primaryStage.close();
});
Button btnSave = new Button("Save");
btnSave.setDefaultButton(true);
btnSave.setOnAction(e -> {
System.out.println("-> Save");
primaryStage.close();
});
ButtonBar buttonBar = new ButtonBar();
buttonBar.getButtons().addAll(btnCancel, btnSave);
root.getChildren().addAll(textField, buttonBar);
primaryStage.setScene(new Scene(root));
primaryStage.setTitle("Consume Test");
primaryStage.show();
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Currently adding a catch to the default button to determine if the TextField has focus:
btnSave.setOnAction(e -> {
if (textField.isFocused()) return;
System.out.println("-> Save");
primaryStage.close();
});
FREQUENCY : always
Attachments
Issue Links
- relates to
-
JDK-8229924 Editable ComboBox: broken sequence in event dispatch on Enter
-
- Open
-
-
JDK-8234247 ComboBox: cancel button must not be triggered if escape is consumed by combo handler
-
- Open
-
-
JDK-8207774 TextField: must not forward ENTER if actionHandler consumed the actionEvent
-
- Resolved
-
- links to
-
Commit openjdk/jfx/d2d44b4a