How to disable automatic layout for a JavaFX Group?

javafx-2

Solution

The question is a bit hard to understand, but I'm guessing you have a Group in a managed layout Pane of some kind (such as a StackPane or VBox) but you don't want the Group's layout values being set by the layout pane -- in which case you can make the group unmanaged.

For example, in the following code, the unmanaged nature of the group causes it to have layout co-ordinates of 0,0 rather than layout co-ordinates of 50,60 which StackPane would usually set to center it inside the parent StackPane.

Group group = new Group(new Rectangle(100, 80));
group.setManaged(false);
StackPane container = new StackPane();
container.setMinSize(StackPane.USE_PREF_SIZE, StackPane.USE_PREF_SIZE);
container.setPrefSize(200, 200);
container.setMaxSize(StackPane.USE_PREF_SIZE, StackPane.USE_PREF_SIZE);
container.getChildren().add(group);

Problem

I have a Group whose coordinate transformations I want to manage completely myself. The built-in `layoutX` and `layoutY` properties are messing this up, and I would like to disable them completely. There seems to be at least two ways of doing this. `relocate(0,0)` or simply setting the properties to 0. But doing this in the `Group` construction is too early; the values are 0 there anyways. So: how do I tell the `Group` to not bother doing layouting? Naturally, I can also leave the layoutX|Y as they are, and compensate for them in my own transforms. That's the plan B. Ref. JavaFX Node

Original source