Pane
Pane is the simplest layout container in JavaFX. It does not automatically arrange its children.
You must manually set the position of each element using setLayoutX() and setLayoutY().
Example:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class PaneExample extends Application {
@Override
public void start(Stage stage) {
Pane pane = new Pane();
Button button1 = new Button("Button 1");
button1.setLayoutX(50);
button1.setLayoutY(30);
Button button2 = new Button("Button 2");
button2.setLayoutX(180);
button2.setLayoutY(100);
pane.getChildren().addAll(button1, button2);
Scene scene = new Scene(pane, 400, 200);
stage.setTitle("Pane Example");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Result:
Button 1 appears at x=50, y=30
Button 2 appears at x=180, y=100
Important methods:
node.setLayoutX(50);
node.setLayoutY(30);
layoutX= distance from the leftlayoutY= distance from the top
Unlike HBox, VBox, or GridPane, Pane does not move or resize elements automatically.
For example:
Pane pane = new Pane();
Label label = new Label("Hello");
label.setLayoutX(100);
label.setLayoutY(50);
pane.getChildren().add(label);
FXML version:
<Pane prefWidth="400" prefHeight="200"
xmlns:fx="http://javafx.com/fxml">
<children>
<Button text="Click Me"
layoutX="100"
layoutY="50"/>
</children>
</Pane>
Typical uses:
- custom drawings
- games
- drag-and-drop interfaces
- when you want exact positions
- placing shapes or images manually
Example with shapes:
Pane pane = new Pane();
Circle circle = new Circle(100, 100, 50);
Rectangle rect = new Rectangle(200, 50, 80, 40);
pane.getChildren().addAll(circle, rect);
Pane is often used as the base for graphics or animation because it gives full control.
Difference from AnchorPane:
| Pane | AnchorPane |
|---|---|
| Uses fixed x/y positions | Uses distances from edges |
| Does not resize automatically | Adjusts when the window changes |
| Manual placement | Responsive placement |
For example, if the window becomes larger:
- In
Pane, the controls stay at the same x/y coordinates. - In
AnchorPane, controls can stay attached to the right, left, top, or bottom.
You usually should prefer layouts like BorderPane, HBox, VBox, or GridPane.
Use Pane only when you really need exact positioning, such as:
- placing wallpaper thumbnails at specific coordinates
- custom monitor preview drawings
- draggable objects