Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX - StackPane X, Y coordinates

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.

like image 350
kingkong Avatar asked Feb 20 '13 15:02

kingkong


People also ask

How to change position in StackPane in JavaFX?

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().

What is StackPane in JavaFX?

StackPane lays out its children in a back-to-front stack. The z-order of the children is defined by the order of the children list with the 0th child being the bottom and last child on top. If a border and/or padding have been set, the children will be layed out within those insets.

How do you make a StackPane?

Java Program to create a StackPane, add circle, label, rectangle and add it to the stage: In this program we are creating a Label named label, a Rectangle named rectangle and a Circle named circle. Then set the font of the StackPane using the setFont() function.

Which one of the panes organizes nodes in the form on top of each other in the center of the pane?

The stack pane layout arranges the nodes in our application on top of another just like in a stack.


1 Answers

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).

like image 64
jewelsea Avatar answered Nov 02 '22 05:11

jewelsea