Simon Jansson
Simon Jansson

Reputation: 61

.setLayoutX() not affecting position in FlowPane

For some reason or another textExampleTwo.setLayoutX(40) does not actually result in the Text moving at all to the right. Is this a bug or have I missed something important here?

public void start(Stage stage) throws Exception {

    FlowPane flowPane = new FlowPane();
    flowPane.setOrientation(Orientation.VERTICAL);

    Text textExampleOne = new Text("An example - 1");

    Text textExampleTwo = new Text("An example - 2");
    textExampleTwo.setLayoutX(40.0);

    flowPane.getChildren().addAll(textExampleOne, textExampleTwo);

    Scene applicationScene = new Scene(flowPane);
    stage.setHeight(400.0);
    stage.setWidth(400.0);
    stage.setScene(applicationScene);
    stage.show();
}

An image of the application (Note that they are on the same X level)

Upvotes: 0

Views: 1765

Answers (1)

fabian
fabian

Reputation: 82451

You've missed something important here:

Many Panes including FlowPane determine the position of their children on their own. For positioning the layoutX and layoutY properties are used. If you assign a new value to one of them and the Node is a child of a layout that positions it's children itself, this just leads to the position to be changed back during the next layout pass.

The exception to this are Nodes with the managed property set to false. This leads to neither layoutX nor layoutY being assigned however.

In your case you seem to want a combination of the two.

In this case the desired effect can be achieved by setting a margin.

// set all insets except the left one to 0
FlowPane.setMargin(textExampleOne, new Insets(0, 0, 0, 40));

Note however that this does not set the x position to 40, but it keeps a space of size 40 at the left of the Node. If you add enough children before this node to move it to the second column, this spacing would be used to calculate the distance to the beginning of the column.

Upvotes: 1

Related Questions