41 JavaFX Pane
The JavaFX Pane class is a layout container which can contain other JavaFX components internally, and lay them out. Actually, the JavaFX Pane class does not actually provide any layout algorithm. The Pane class simply displays the components it contains at the locations the components themselves want to be located. In other words, the Pane class uses the layoutX
and layoutY
specified by its child components to determine where to display them.
The JavaFX Pane class, javafx.scene.layout.Pane
, is a subclass of the JavaFX Region class, so it inherits all of the Region class functionality. That includes functionality like borders, padding, background settings etc.
Create a JavaFX Pane
You create a JavaFX Pane simply via its standard no-arg constructor. Here is an example of creating a JavaFX Pane instance:
Pane pane = new Pane();
Add Items to a JavaFX Pane
You add items JavaFX Pane by obtaining its list of children via getChildren()
, and then add the items to that list. Here is an example of adding a JavaFX Label to a JavaFX Pane:
Pane pane = new Pane(); pane.getChildren().add(new Label("Hello Pane"));
If you repeat the last line multiple times, you will add multiple Label instances to the Pane. Just keep in mind, that unless you change the layoutX and / or layoutY properties of the added Labels, all the Label instances will be displayed in the same X and Y position - meaning on top of each other.
Adding a JavaFX Pane to the Scene Graph
Here is an example of adding a JavaFX Pane to the JavaFX scene graph - by setting the Pane as the root node of a JavaFX Scene:
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.Pane; import javafx.stage.Stage; public class PaneExample extends Application { public static void main(String[] args) { launch(args); } public void start(Stage primaryStage) { Pane pane = new Pane(); pane.getChildren().add(new Label("Hello Pane")); Scene scene = new Scene(pane); primaryStage.setScene(scene); primaryStage.show(); } }