Luiz
Luiz

Reputation: 1

how to make all cells in GridPane visible and same size?

I've written the code below and that's the output. Is it possible to make all of the rows and columns(cells) visible and to make all of them certain size? The result I want is to have some sort of "net", so I can put in something into a cell and be sure that the size of all table will remain the same.

GridPane.setVgap() and GridPane.setMinSize() is not what I'm looking for.

enter image description here

package sample;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.control.Button;

public class Main extends Application {
    Stage window;

    @Override
    public void start(Stage primaryStage) throws Exception{
        window = primaryStage;
        GridPane grid = new GridPane();

        //buttons
        Button btn = new Button("8,5");
        GridPane.setConstraints(btn, 8,5);
        Button btn2 = new Button("1,3");
        GridPane.setConstraints(btn2, 1,3);

        grid.getChildren().addAll(btn, btn2);

        grid.setGridLinesVisible(true);
        Scene scene = new Scene(grid, 600,400);
        window.setScene(scene);
        window.show();
    }


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

Thanks in advance!

Upvotes: 0

Views: 3582

Answers (1)

fabian
fabian

Reputation: 82531

You cannot span an infinite grid with GridPane. That's just not what the layout is designed for. The gridLinesVisible feature exists for debugging purposes only anyways.

You can achieve cells with the same sizes though, if you know the number of rows/columns though: Use RowConstraints/ColumnConstraints with percentHeight/Width to give every row/column the same size:

int rowCount = 6;
int columnCount = 9;

RowConstraints rc = new RowConstraints();
rc.setPercentHeight(100d / rowCount);

for (int i = 0; i < rowCount; i++) {
    grid.getRowConstraints().add(rc);
}

ColumnConstraints cc = new ColumnConstraints();
cc.setPercentWidth(100d / columnCount);

for (int i = 0; i < columnCount; i++) {
    grid.getColumnConstraints().add(cc);
}

Upvotes: 3

Related Questions