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

Text node translateXProperty()/translateYProperty() binding not working when text or font set after binding

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8, 9
    • javafx
    • I'm using JavaFX 8u45 on a 64-bit Windows 7 environment.

      I've created a Text node, as follows:

      Text text = new Text();

      I then bind its xProperty(), yProperty(), translateXProperty(), and translateYProperty() to achieve centering, as follows:

      text.xProperty().bind(scene.widthProperty().divide(2));
      text.yProperty().bind(scene.heightProperty().divide(2));
      text.translateXProperty().bind(new SimpleDoubleProperty(-text.layoutBoundsProperty().get().getWidth()/2));
      text.translateYProperty().bind(new SimpleDoubleProperty(-text.layoutBoundsProperty().get().getHeight()/2));

      Finally, I set the text origin, a font, and the text, as follows:

      text.setTextOrigin(VPos.TOP);
      text.setFont(new Font("Arial BOLD", 14));
      text.setText("Center Text");

      I've noticed that the text is not horizontally centered. However, the left edge of the text is located at the center of the scene. If I move the setFont() and setText() calls to before text.xProperty(), the text is horizontally centered.

      I'm not sure if I've done something wrong or if this is a bug. Here is a short program to demonstrate:

      import javafx.application.Application;

      import javafx.beans.property.SimpleDoubleProperty;

      import javafx.geometry.VPos;

      import javafx.scene.Group;
      import javafx.scene.Scene;

      import javafx.scene.shape.Circle;

      import javafx.scene.text.Font;
      import javafx.scene.text.Text;

      import javafx.stage.Stage;

      public class Center extends Application
      {
         @Override
         public void start(Stage primaryStage)
         {
            primaryStage.setTitle("Center Text");
            primaryStage.setWidth(250);
            primaryStage.setHeight(250);

            Group root = new Group();
            Scene scene = new Scene(root);

            Text text = new Text();
      // text.setFont(new Font("Arial BOLD", 14));
      // text.setText("Center Text");
            text.xProperty().bind(scene.widthProperty().divide(2));
            text.yProperty().bind(scene.heightProperty().divide(2));
            text.translateXProperty().bind(new SimpleDoubleProperty(-text.layoutBoundsProperty().get().getWidth()/2));
            text.translateYProperty().bind(new SimpleDoubleProperty(-text.layoutBoundsProperty().get().getHeight()/2));
            text.setTextOrigin(VPos.TOP);
      // Comment out the following two lines when uncommenting the previous commented-out lines.
            text.setFont(new Font("Arial BOLD", 14));
            text.setText("Center Text");

            root.getChildren().add(text);

            primaryStage.setScene(scene);
            primaryStage.show();
         }
      }

      The program is currently set up so you should not see horizontal centering.

      Jeff

            Unassigned Unassigned
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Imported: