JavaFX - StackPane X, Y coordinates


Question

I'm using StackPanel as container for my figures, panels, etc. What I discovered, that coordinates X,Y (0,0) are placed right in center of my panel.

Is it possible to move it to left top od Pane ? Calculating all dimensions from center is much more difficult.

1
5
2/20/2013 3:31:57 PM

Accepted Answer

You can set the layout of Nodes added to the StackPane to a position within the Stackpane using the StackPane.setAlignment(node, position) method:

Label topLeftLabel = new Label("Top Left");
StackPane stack = new StackPane();
stack.getChildren().add(topLeftLabel);

StackPane.setAlignment(topLeftLabel, Pos.TOP_LEFT);

Even though this is possible, from your brief description of how you are trying to use the StackPane, it sounds like you would be better off using a regular Pane, or a Group or an AnchorPane for the kind of absolute positioning you appear to be wanting to achieve.

Possibly look into using a visual tool such as SceneBuilder as well. Even if you don't end up using the FXML it outputs, SceneBuilder should give you a much better idea of how JavaFX layout mechanisms work. SceneBuilder makes use of AnchorPane as its default layout pane used to provide absolute positioning for elements (which seems to be what you want to achieve).

13
2/20/2013 8:31:47 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon