1 Reply Latest reply: Jan 12, 2013 6:39 AM by FXit RSS

    Setting Caret Position in TextField when Field changed

    editOr
      Hi,

      I'd like to position the curser at the beginning of the textFields text after every entry. I know there is a method "positionCaret". In the example below this does not seem to work when used in a changeListener.

      Hints?
      import javafx.application.Application;
      import javafx.beans.value.ChangeListener;
      import javafx.beans.value.ObservableValue;
      import javafx.scene.Scene;
      import javafx.scene.control.TextField;
      import javafx.scene.layout.BorderPane;
      import javafx.stage.Stage;
      
      public class CaretPositioner extends Application{
      
           public static void main(String[] args) {
                Application.launch((java.lang.String[])null);
           }
      
           @Override
           public void start(Stage primaryStage) {
      
                BorderPane root = new BorderPane();   
                TextField tf = new TextField();
                tf.textProperty().addListener(new CaretChanger(tf));
      
                root.setCenter(tf);
                Scene scene = new Scene(root);
                primaryStage.setScene(scene);
                primaryStage.show();           
           }
      
           class CaretChanger implements ChangeListener<String>{
      
                private TextField tf;
      
                CaretChanger(TextField tf){
                     this.tf = tf;
                }
      
                @Override
                public void changed(ObservableValue<? extends String> observable,
                          String oldValue, String newValue) {
                     System.out.println(tf.caretPositionProperty().get());
                     tf.positionCaret(0);
                }
           }
      
      }
      Edited by: 979705 on 12.01.2013 00:45

      Edited by: 979705 on 12.01.2013 00:46
        • 1. Re: Setting Caret Position in TextField when Field changed
          FXit
          Hi,

          I didn't get it work too, but this workaround seems to work.

          Cheers

           
          
          import javafx.application.Application;
          import javafx.application.Platform;
          import javafx.beans.value.ChangeListener;
          import javafx.beans.value.ObservableValue;
          import javafx.scene.Scene;
          import javafx.scene.control.TextField;
          import javafx.scene.layout.BorderPane;
          import javafx.stage.Stage;
          
          public class CaretPositioner extends Application {
          
               public static void main(String[] args) {
                    Application.launch((java.lang.String[]) null);
               }
          
               @Override
               public void start(Stage primaryStage) {
          
                    BorderPane root = new BorderPane();
                    final TextField tf = new TextField();
          
                    tf.textProperty().addListener(new ChangeListener<String>() {
                         @Override
                         public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
                              System.out.println(tf.getCaretPosition());
                              Platform.runLater(new Runnable() {
                                   @Override
                                   public void run() {
                                        tf.positionCaret(0);
                                   }
                              });
          
                         }
                    });
          
                    root.setCenter(tf);
                    Scene scene = new Scene(root);
                    primaryStage.setScene(scene);
                    primaryStage.show();
               }
          }