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

TextFormatter filter getting bypassed.

XMLWordPrintable

    • x86_64
    • generic

      A DESCRIPTION OF THE PROBLEM :
      When trying to create a suffix filter via TextFormatter.
      1. It allows positioning caret between first and second character of suffix via mouse click only, which should not be possible at all.
      2. Even using getCaretPosition() does not reveal exact caret position in this specific case.
      2. Positioning caret between other characters of suffix is disallowed which is an expected behaviour

      REGRESSION : Last worked in version 8u172

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Run the below source code and try clicking in the middle of first and second character, in the below case it would be "P" and "C", do it very precisely.
      2. If you fill some text lets say 100 and it shows as "100PCS" and then try clicking between "P" and "C", the caret will position itself between "1" and "0".
      ===================================================
      The source code provided has some code commented, if that code is uncommented, a thread will print the carets position after two seconds, which can be used to debug the problem

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Caret should not get positioned between the suffix text if clicked between them.
      The behaviour should be same as it is for clicking between "C" and "S".
      ACTUAL -
      Caret gets positioned between "P" and "C", should not happen.
      No error messages or anything as such.
      =====================================================
      Use the video below to see the actual result.
      https://www.youtube.com/watch?v=ZqjZR03d2VU
      @00:04 Clicked between "P" and "C", caret position changes. (Errorneous Behaviour)
      @00:08 Clicked between "C" and "S"
      @00:10 Clicked before "P"
      @00:13 Again Clicked between "P" and "C", caret position changes. (Errorneous Behaviour)
      @00:19 Caret position lies between "P" and "C", typed "1","0","0".
      @00:30 Caret position lies between "0" and "P", again clicked between "P" and "C", but now caret position doesn't change.
      @00:36 Caret position lies between "0" and "P", clicked between "C" and "S", caret position doesn't change.
      @00:39 Caret position lies between "0" and "P", clicked before "1", caret position changes accordingly to before "1".
      @00:43 Caret position is before "1", clicked between "C" and "S", caret position changes to between "1" and "0".(Errorneous Behaviour)
      @00:48 Clicked before "1", caret position changes accordingly to before "1".
      @00:52 Caret position is before "1", clicked between "P" and "C", caret position changes to between "1" and "0".(Errorneous Behaviour)

      ---------- BEGIN SOURCE ----------
      import java.util.function.UnaryOperator;

      import javafx.application.Application;
      import javafx.concurrent.Service;
      import javafx.concurrent.Task;
      import javafx.scene.Scene;
      import javafx.scene.control.TextField;
      import javafx.scene.control.TextFormatter;
      import javafx.scene.layout.StackPane;
      import javafx.scene.text.Font;
      import javafx.stage.Stage;

      public class SingleIndRun2 extends Application {

          public TextField txtFldObj;
          public Service serviceObj;

          public SingleIndRun2() {

              /*serviceObj = new Service() {
                  @Override
                  protected Task createTask() {
                      return new Task() {
                          @Override
                          protected Void call() throws Exception {
                              System.out.println("Thread started, Waiting");
                              Thread.sleep(2000);
                              System.out.println("Thread started, Executing. txtFldObj.getCaretPosition():"+txtFldObj.getCaretPosition()+", txtFldObj.getAnchor():"+txtFldObj.getAnchor());
                              //txtFldObj.end();
                              return null;
                          }
                      };
                  }
              };*/
          }

          private TextField createFixedPrefixTextField(String suffix) {
              txtFldObj = new TextField(suffix);
              txtFldObj.setFont(new Font("Arial", 30));

              UnaryOperator<TextFormatter.Change> filter = c -> {
                  if (c.getCaretPosition() > (c.getControlNewText().length() - suffix.length())
                          || c.getAnchor() > (c.getControlNewText().length() - suffix.length())) {
                      System.err.println("Returning Null. c.getCaretPosition():" + c.getCaretPosition()+",c.getAnchor():" + c.getAnchor()+"....."+"controlNewTextLength-SuffixLength:" + (c.getControlNewText().length() - suffix.length()));
                      /*if (!serviceObj.isRunning()) {
                          serviceObj.reset();
                          serviceObj.start();
                      }*/
                      return null;
                  } else {
                      System.out.println("Valid");
                      return c;
                  }
              };

              txtFldObj.setTextFormatter(new TextFormatter<>(filter));
              txtFldObj.positionCaret(0);

              return txtFldObj;
          }

          @Override
          public void start(Stage primaryStage) {
              txtFldObj = createFixedPrefixTextField("PCS");
              StackPane root = new StackPane(txtFldObj);
              Scene scene = new Scene(root, 300, 40);
              primaryStage.setScene(scene);
              primaryStage.show();
          }

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

      CUSTOMER SUBMITTED WORKAROUND :
      I have tried getting caretposition to move it back to the correct place, but the system shows that the caret is already in the correct place.

      FREQUENCY : often


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

              Created:
              Updated: