GridPane unintended row resize

62 views Asked by At

I've created GridPane containing panels. Every panel is Pane. Click on every panel should add a point (Circle) to the panel. A problem emerges when adding the point to the bottom border of the panel which resizes the panel. I can't see any particular reason for it to happen while the size of the panel (Pane) is not affected in any way. I seek for an explanation and possible fix. For the demo reasons only the top panel is point addable.

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class GridPaneApp extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        int columns = 1;
        int rows = 2;

        GridPane gridPane = new GridPane();
        gridPane.setHgap(4);
        gridPane.setVgap(4);
        gridPane.setPadding(new Insets(4));

        for (int i = 0; i < columns; i++) {
            gridPane.getColumnConstraints().add(new ColumnConstraints(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE, Priority.ALWAYS, HPos.CENTER, true));
        }

        for (int i = 0; i < rows; i++) {
            gridPane.getRowConstraints().add(new RowConstraints(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE, Priority.ALWAYS, VPos.CENTER, true));
        }

        for (int columnIndex = 0; columnIndex < columns; columnIndex++) {
            for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
                Pane pane = new Pane();
                pane.setStyle("-fx-background-color: purple, white; -fx-background-insets: 0, 1;");
                gridPane.getChildren().add(pane);
                GridPane.setConstraints(pane, columnIndex, rowIndex, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
                if (rowIndex == 0) {
                    pane.setOnMousePressed(event -> {
                        double x = event.getX();
                        double y = event.getY();
                        System.out.println("x=" + x + ", y=" + y);
                        Circle circle = new Circle(8, Color.PURPLE);
                        circle.setLayoutX(x);
                        circle.setLayoutY(y);
                        pane.getChildren().add(circle);
                    });
                }
            }
        }

        Scene scene = new Scene(gridPane, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
0

There are 0 answers