Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8207759

VK_ENTER not consumed by TextField when default Button exists

    Details

    • Subcomponent:
    • Introduced In Build:
      b82
    • Introduced In Version:
      9
    • CPU:
      x86_64
    • OS:
      generic

      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


        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                fastegal Jeanette Winzenburg
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: