JavaFX: Absolute Positioning Using Pane

This entry is part 13 of 55 in the series JavaFX

We have looked at StackPane and BorderPane.  The most basic layout pane in JavaFX is the Pane class itself.  Pane class is also the parent class of StackPane.

Pane allows absolute positioning of its children.  When using Pane, we have to specify its absolute position.

Consider the following example where I have used relocate method to position the 2 elements including the Text object.

The rect.relocate(70,100) is the relative x, y position of the rectangle from the top left corner of the pane.  You can try adjusting the x,y values to see how they affect the position of the rectangle.

package javafxapplication12;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class JavaFXApplication12 extends Application {

    public void start(Stage stage) {

        stage.setTitle("JavaFX Program!");

        Pane pane = new Pane();

        Scene scene = new Scene(pane, 500, 400);

        pane.setStyle("-fx-background-color: grey;");

        Rectangle rect = new Rectangle(100, 100, Color.ORANGE);
        rect.relocate(70, 100);

        Text txt = new Text();
        txt.setFont(Font.font("Times New Roman", 72));
        txt.setText("Hello World");
        txt.relocate(90, 150);

        pane.getChildren().addAll(rect, txt);;

    public static void main(String[] args) {



Series Navigation<< JavaFX: Putting StackPane in HBox
JavaFX: Using Label with Graphics >>

Leave a comment

Leave a Reply