How to use the JavaFX Pane
When absolute positioning is needed, you may use the JavaFX Pane. By default, it places the nodes at the 0,0 axes, and you need to set the position of the nodes manually. The Pane resizes all resizable children to their preferred sizes. When you have a node in the Pane, it will set its preferred size to the same as the node. You can also tell the Pane to compute its preferred size based on its children.
The following example will show you how to create the JavaFX Pane, add nodes in the Pane, set the position of the nodes, and use the computed size of the Pane based on the children’s sizes. Proceed to the example below to see what that looks like.
Example
import java.io.IOException; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.Pane; import javafx.scene.layout.Region; import javafx.stage.Stage; /** * * @author KENSOFT */ public class JavaFXLayout extends Application { @Override public void start(Stage stage) throws IOException { Button btn1 = new Button("Button 1"); Button btn2 = new Button("Button 2"); btn1.relocate(10, 10); btn2.relocate(100, 10); Pane root = new Pane(); root.getChildren().addAll(btn1, btn2); root.setStyle("-fx-border-style: solid inside;" + "-fx-border-width: 5;" + "-fx-border-color: red;"); // Added a border to see the difference with computed size root.setPrefSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE); Scene scene = new Scene(root); stage.setScene(scene); stage.setTitle("Pane"); stage.show(); } public static void main(String[] args) { launch(args); } }