JavaFX: how to host a component / node?

In JavaFX, is there something similar to setLayout(); or setBounds(); ?

For example, I want to position the button in the desired position.

+5
source share
3 answers

Everything on the JavaFX scene graphics is Node . Each node has an X coordinate and a Y coordinate. But there are several ways for the set/change position child component. It depends on the layout manager used to display the component on the scene graph.

  • There are layout managers like Group that don’t calculate the default position of the child, and you can use layoutX and layoutY directly on them
  • There are other layout managers, such as Region , which automatically calculate the position of the default child using layoutX and inorder to adjust the location of components from the default positions, you need to use translateX and translateY values.

From the docs:

If the node is managed and has the region as the parent, then the layout area will set layoutX according to its own layout policy. If the node is unmanaged or parent to the group, the application can install layoutX directly to host it.

+8
source

You should read the Node class (long text at the beginning), and then, especially relocate , setLayoutX (and Y) and setTranslateX (and Y).

0
source

In addition to what others have already mentioned, if you could put the ur button (or any other node) in the stack area, you could use the Stackpane alignment property, which accepts javafx.geometry.Pos β€” the alignment of the child within the stackpane. For example, in your case:

 <StackPane > <Button translateY="-15" translateX="15" StackPane.alignment="TOP_RIGHT"/> </StackPane> 
0
source

All Articles