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

TextField: all text content must be selected initially

XMLWordPrintable

    • b15

      Below is an example that inserts a TextField into a parent while the showing.

      Run and:
      - click add to insert the textField
      - expected: textfield inserted and all its content selected
      - actual: content not selected

      to see that this is a first-time misbehavior only:
      - click on remove
      - click on add
      - expected and actual: all content selected

      The culprit: TextFieldBehavior

      in its constructor, it installs a listener to scene.focusOwner which handles the selectAll on change but does not handle the intial state:

              textField.sceneProperty().addListener(weakSceneListener);
              if (textField.getScene() != null) {
                  textField.getScene().focusOwnerProperty().addListener(weakFocusOwnerListener);
                  // FIXME bug or feature: initial selection?
                  if (textField.getScene().getFocusOwner() == textField) {
                  // missing line
      // textField.selectRange(textField.getLength(), 0);
                  }
              }

      also might be that the re-wiring code (when switching the scene) should update the selection


      The example:

      public class TextFieldInitialSelection extends Application {

          private Parent createContent() {
              TextField textField = new TextField("just some text");
              Button add = new Button("insert");
              Button remove = new Button("remove");
              VBox content = new VBox(10, add, remove);
              
              add.setOnAction(e -> {
                  content.getChildren().add(textField);
                  textField.requestFocus();
              });
              remove.setOnAction(e -> {
                  content.getChildren().remove(textField);
              });
              return content;
          }

          @Override
          public void start(Stage stage) throws Exception {
              stage.setScene(new Scene(createContent(), 300, 300));
              stage.show();
          }

          public static void main(String[] args) {
              launch(args);
          }
      }

            kpk Karthik P K
            fastegal Jeanette Winzenburg
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: