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

Label width is often off by one. Bounds calculation results in poor right alignment.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 8
    • 7u25
    • javafx
    • Windows 7
      Java 7u25

      Look at the second label when running this test code. Trying to right-align Labels is impossible.

      package rightalignedlabels;

      import javafx.application.Application;
      import javafx.geometry.Pos;
      import javafx.scene.Group;
      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.scene.control.ContentDisplay;
      import javafx.scene.control.Label;
      import javafx.scene.layout.BorderPane;
      import javafx.scene.layout.HBox;
      import javafx.scene.layout.VBox;
      import javafx.scene.paint.Color;
      import javafx.scene.shape.Arc;
      import javafx.stage.Stage;

      public class RightAlignedLabels extends Application {

      @Override
      public void start(Stage primaryStage) {
      VBox vbox = new VBox();
      vbox.setAlignment(Pos.CENTER_RIGHT);
      vbox.setFillWidth(false);
      vbox.setStyle("-fx-border-color: purple; -fx-border-width: 1;");
      for (int i = 0; i < 4; i++) {
      doLabelBox(i, vbox);
      }

      Parent group = new Group(vbox);
      group.setScaleX(5.0);
      group.setScaleY(5.0);

      BorderPane root = new BorderPane();
      root.setCenter(group);

      Scene scene = new Scene(root);

      primaryStage.setTitle("Right-aligned Labels look bad");
      primaryStage.setScene(scene);
      primaryStage.setWidth(550);
      primaryStage.setHeight(500);
      primaryStage.show();
      }


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

      private static String alphabet = "cdefghijklmno";//pqrstuvwxyz";
      private void doLabelBox(int i, VBox vbox) {
      HBox hbox = new HBox();
      hbox.setAlignment(Pos.BASELINE_RIGHT);
      hbox.setStyle("-fx-border-color: green; -fx-border-width: 1;");
      Arc arc = new Arc();
      arc.setRadiusX(4);
      arc.setRadiusY(4);
      arc.setStartAngle(-90);
      arc.setLength(180);
      arc.setStroke(Color.BLACK);
      arc.setFill(Color.YELLOW);
      Label label = new Label("("+alphabet.substring(i)+")", arc);
      label.setContentDisplay(ContentDisplay.RIGHT);
      label.setStyle("-fx-border-color: orange; -fx-border-width: 1;");
      label.setTranslateX(8);
      hbox.getChildren().add(label);
      vbox.getChildren().add(hbox);
      }
      }

        1. after-patch.jpg
          91 kB
          Jonathan Giles
        2. before-patch.jpg
          91 kB
          Jonathan Giles

            jgiles Jonathan Giles
            swpalmer Scott Palmer
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: