1 Reply Latest reply: Sep 4, 2013 7:58 AM by PavelSafrata RSS

    Adding dropshadow on label affects layout


      I have observed a cusious behavior when combining a DropShadow effect with a perspective transformation. The set up is as follows:

      There are labels in a VBox. The labels can get a drop shadow effect applied to them based on an external event. The whole VBox then has the effect PerspectiveTransformation applied to it. What now happens if the effect on the label changes, all the other labels in the VBox change their position.

      import javafx.application.Application;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.Label;
      import javafx.scene.effect.DropShadow;
      import javafx.scene.effect.PerspectiveTransform;
      import javafx.scene.effect.PerspectiveTransformBuilder;
      import javafx.scene.layout.StackPane;
      import javafx.scene.layout.VBox;
      import javafx.scene.paint.Color;
      import javafx.scene.shape.Rectangle;
      import javafx.stage.Stage;
      public class NewFXMain extends Application {
          public void start(Stage primaryStage) {
              Label l1 = new Label("Label1");
              Label l2 = new Label("Label2");
              Label l3 = new Label("Label3");
              Label l4 = new Label("Label4");
              final Label l5 = new Label("Label5");
              final Label l6 = new Label("Label6");
              final DropShadow ds = new DropShadow();
              Button btn = new Button();
              btn.setText("Add drop shadow");
              btn.setOnAction(new EventHandler<ActionEvent>() {
                  public void handle(ActionEvent event) {
                      if (l5.getEffect() == null) {
                      } else {
              VBox box = new VBox(l1, l2, l3, l4, l5, l6);
              PerspectiveTransform perspectiveTransform = PerspectiveTransformBuilder.create()
              // To visualize the transformation
              Rectangle rect1 = new Rectangle(10, 10, 150, 150);
              Rectangle rect2 = new Rectangle(10, 10, 150, 150);
              StackPane root = new StackPane();
              root.getChildren().addAll(box, rect1, rect2, btn);
              Scene scene = new Scene(root, 300, 250);
              primaryStage.setTitle("Hello World!");
          public static void main(String[] args) {

      One way to avoid this would be to put each element on a stack pane/group and manage the positioning by hand. Any other good ideas? Does anybody know why this happens (does not happen without the effect on the VBox)?

      By the way this happens in both JavaFX 2.2 and JavaFX 8.

        • 1. Re: Adding dropshadow on label affects layout

          It doesn't affect layout, it affects the PerspectiveTransform effect which - unlike layouts - takes the effects of the nested nodes into account. In another words - the labels are laid out still the same regardless of the shadow, but then the perspective transform takes the entire bounds including the shadow (different than bounds without the shadow) and computes the result.

          I would recommend setting enough padding to the perspective-transformed VBox to accommodate the shadow effect change so that the box's bounds that the perspective transform uses are still the same.


          box.setPadding(new Insets(10));