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

Programmatic TextField selection doesn't update control

XMLWordPrintable

    • x86_64
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Arch Linux with Oracle JDK package https://aur.archlinux.org/packages/jdk/

      A DESCRIPTION OF THE PROBLEM :
      Selecting textfield text using the selection methods such as selectPositionCaret, selectEnd, and selectAll doesn't update the selection shown in the control.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create sample JavaFX project using provided source code, run it, and type a few characters into the text field after the existing text.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      When a character is typed into textfield after existing text, expect to see anchor move to before first character of TextField and blue selection indicator move over letters "Se".
      ACTUAL -
      As characters are typed into textfield, no selection is shown and the caret stays after the last character typed. The println output shows the caret/anchor and selected text as expected, though:

      caret 2
      anchor 0
      selected text So

      ---------- BEGIN SOURCE ----------
      <?import javafx.geometry.Insets?>
      <?import javafx.scene.layout.GridPane?>

      <?import javafx.scene.control.Button?>
      <?import javafx.scene.control.Label?>
      <?import javafx.scene.control.TextField?>
      <GridPane fx:controller="sample.Controller"
                xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
          <TextField fx:id="textField"/>
      </GridPane>

      package sample;

      import javafx.fxml.FXML;
      import javafx.fxml.Initializable;
      import javafx.scene.control.TextField;

      import java.awt.*;
      import java.net.URL;
      import java.util.ResourceBundle;

      public class Controller implements Initializable {
          @FXML
          private TextField textField;

          @Override
          public void initialize(URL location, ResourceBundle resourceBundle) {
              textField.setText("Some Text");

              textField.textProperty().addListener((observableValue, aBoolean, t1) -> {
                  textField.positionCaret(0);
                  textField.selectPositionCaret(2);
                  System.out.println("caret " + textField.getCaretPosition());
                  System.out.println("anchor " + textField.getAnchor());
                  System.out.println("selected text " + textField.getSelectedText());
              });
          }
      }
      package sample;

      import javafx.fxml.FXML;
      import javafx.fxml.Initializable;
      import javafx.scene.control.TextField;

      import java.awt.*;
      import java.net.URL;
      import java.util.ResourceBundle;

      public class Controller implements Initializable {
          @FXML
          private TextField textField;

          @Override
          public void initialize(URL location, ResourceBundle resourceBundle) {
              textField.setText("Some Text");

              textField.textProperty().addListener((observableValue, aBoolean, t1) -> {
                  textField.positionCaret(0);
                  textField.selectPositionCaret(2);
                  System.out.println("caret " + textField.getCaretPosition());
                  System.out.println("anchor " + textField.getAnchor());
                  System.out.println("selected text " + textField.getSelectedText());
              });
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      No workaround identified

      FREQUENCY : always


        1. Controller.java
          0.8 kB
        2. Main.java
          0.6 kB
        3. main.fxml
          0.4 kB

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: