11

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

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

Quijibo
  • 307
  • 2
  • 13
kingkong
  • 1,537
  • 6
  • 27
  • 48

2 Answers2

20

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

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • I'm having the same issue. I would prefer to use a StackPane, though, because I want to place things on top. How would I overlay nodes while still positioning them exactly where I want? – qwerty Jul 23 '20 at 21:53
  • 2
    Put the nodes in the child list in the order you want them painted. All of the layout panes position in 2D space, not 3D space. All panes overlay nodes in the order of the child list for the pane. JavaFX uses the [painter's algorithm](https://en.wikipedia.org/wiki/Painter%27s_algorithm) to render the nodes in this order. Additionally, you can set a Z co-ordinate if you have depth sorting and 3D enabled, but this is not a common case. See: [how to overlap a shape onto another](https://stackoverflow.com/questions/45089396/javafx-how-to-overlap-a-shape-onto-another). – jewelsea Jul 23 '20 at 22:49
  • 2
    To place nodes where you like, you can use a standard Pane, an AnchorPane or a Group. Those nodes allow absolute positioning for items rather than a StackPane which performs relative positioning, usually in the center, but also to the sides or corners if you set alignmee Ed by on the child nodes. – jewelsea Jul 26 '20 at 02:12
0

The previous answer is of course the best in this situation, but it is also wise to know that you can move Nodes on the StackPane using Translation.

Ex.

    Label topLeftLabel = new Label("Top Left");
    StackPane stack = new StackPane();
    stack.getChildren().add(topLeftLabel);
    
    topLeftLabel.setTranslateX(stack.getWidth()/2);
    topLeftLabel.setTranslateY(stack.getHeight()/2);

It would do the same thing (but may look a bit worse)

Mateusz Niedbal
  • 326
  • 4
  • 15
  • `stackPane.setAlignment(Pos.TOP_LEFT);` then label's relative position will be calculated based on top left as (0, 0). – Nick Dong May 29 '22 at 15:55