JavaFX coordinates - StackPane X, Y

I use StackPanel as a container for my shapes, panels, etc. I found that the X, Y coordinates (0,0) are located right in the center of my panel.

Can I move it to the upper left panel? Calculating all sizes from the center is much more complicated.

+4
source share
1 answer

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

Label topLeftLabel = new Label("Top Left"); StackPane stack = new StackPane(); stack.getChildren().add(topLeftLabel); StackPane.setAlignment(topLeftLabel, Pos.TOP_LEFT); 

Even if possible, from your brief description of how you are trying to use StackPane, it looks like you would be better off using regular Pane , or Group or AnchorPane for such absolute positioning, wanting to achieve.

Perhaps look at using a visual tool like SceneBuilder . Even if you are not using the FXML that it outputs, SceneBuilder should give you a much better idea of ​​how JavaFX layout mechanisms work. SceneBuilder uses AnchorPane as the default layout panel used to ensure absolute positioning of elements (which seems to be what you want to achieve).

+11
source

All Articles