Question

I have a program short that puts Rotated objects inside a VBox. I want the VBox to adjust it's vertical size based on the rotation. In the following example the Text objects should NOT touch:

enter image description here

But they do. The code to generate this is below:

package metcarob.com.soavisualise;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class TestAppForRotate extends Application {

@Override
public void start(final Stage primaryStage) throws Exception {

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

    primaryStage.setScene(scene);

    VBox vbox = new VBox();
    root.getChildren().add(vbox);

    Group g = null;
    Text t = null;
    StackPane p = null;

    for (int c=0;c<3;c++) {
        g = new Group();
        t = new Text("Test " + Integer.toString(c));
        p = new StackPane();
        p.getChildren().add(t);
        p.setStyle("-fx-background-color: white; -fx-border-color: orange; -fx-border-width: 3;");
        g.getChildren().add(p);
        g.setRotate(c * 35);
        vbox.getChildren().add(g);
    }

    primaryStage.show();
}

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

In this configuration I have Text inside StackPane inside Group the whole Group is rotated and added to the VBox (In this example I need the Orange Box to rotate as well as the Text)

Any ideas on how to get this to work? Robert

Was it helpful?

Solution

Wrap your rotated nodes in an unrotated Group.

vbox.getChildren().add(new Group(g));

spacing

The reason this works is from the Group javadoc:

if transforms and effects are set directly on children of this Group, those will be included in this Group's layout bounds.

If you want additional fixed space between VBox items, you can use vbox.setSpacing as suggested in Rohan's answer.

OTHER TIPS

I think the best way would be to set spacing.

vbox.setSpacing(double arg0);

You can also adjust spacing in your loop in order to get some dynamic effect, if needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top