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

TextField: all text content must be selected initially

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 8u301, openjfx11.0.2, openjfx17
    • Fix Version/s: tbd
    • Component/s: javafx

      Description

      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);
          }
      }

        Attachments

          Activity

            People

            Assignee:
            aghaisas Ajit Ghaisas
            Reporter:
            fastegal Jeanette Winzenburg
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: