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

Right-aligned text fields position text incorrectly when text changed while receiving focus

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • 7u6
    • fx2.1
    • javafx

      While working on a "MoneyField" sample I encountered the following bug which appears to make it impossible for me to use right-aligned text field as the implementation. As you change focus, the text is no longer right aligned, and if you change focus and then type and tab out of the text field, the text is scrolled off to the right. The bug for some reason happens due to the fact that I am changing the text when focus changes (so that the $ is removed or added based on whether the field has focus).

      public class RightAlignedTextApplication extends Application {
          @Override public void start(Stage stage) throws Exception {
              final TextField field = new TextField();
              field.setAlignment(Pos.BASELINE_RIGHT);
              field.setText("$2.50");

              final TextField field2 = new TextField();
              field2.setAlignment(Pos.BASELINE_RIGHT);
              field2.setText("$2.50");
              
              field.focusedProperty().addListener(new InvalidationListener() {
                  @Override public void invalidated(Observable observable) {
                      if (field.isFocused()) {
                          field.setText(field.getText().substring(1));
                      } else {
                          field.setText("$" + field.getText());
                      }
                  }
              });
              field2.focusedProperty().addListener(new InvalidationListener() {
                  @Override public void invalidated(Observable observable) {
                      if (field2.isFocused()) {
                          field2.setText(field2.getText().substring(1));
                      } else {
                          field2.setText("$" + field2.getText());
                      }
                  }
              });

              VBox root = new VBox();
              root.setFillWidth(false);
              root.setAlignment(Pos.CENTER);
              root.getChildren().addAll(field, field2);
              stage.setScene(new Scene(root, 640, 480));
              stage.show();
          }

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

            leifs Leif Samuelsson (Inactive)
            rbair Richard Bair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: