5

I was wondering if it is at all possible to decide the number of rows and columns a gridpane should have.

Johan Rovala
  • 174
  • 1
  • 2
  • 14
  • 1
    The number of columns and rows is determined by the nodes you add and the `columnIndex` and `rowIndex` you set on them. Your question is not very clear: can you elaborate? – James_D Apr 13 '15 at 16:05
  • I know my question looks pretty bad. What I'm hoping to achieve is to create a grid base layout that shows all the lines of the grid. so say for example i wanted to create a grid layout with 300 lines, then i would want to set the column span to 300. I hope that makes more sense – Johan Rovala Apr 13 '15 at 17:37

1 Answers1

11

You can add the required number of ColumnConstraints and RowConstraints to the GridPane. For example:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;

public class GridPaneForceColsAndRows extends Application {

    @Override
    public void start(Stage primaryStage) {
        GridPane root = new GridPane();
        root.setGridLinesVisible(true);
        final int numCols = 50 ;
        final int numRows = 50 ;
        for (int i = 0; i < numCols; i++) {
            ColumnConstraints colConst = new ColumnConstraints();
            colConst.setPercentWidth(100.0 / numCols);
            root.getColumnConstraints().add(colConst);
        }
        for (int i = 0; i < numRows; i++) {
            RowConstraints rowConst = new RowConstraints();
            rowConst.setPercentHeight(100.0 / numRows);
            root.getRowConstraints().add(rowConst);         
        }
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
James_D
  • 201,275
  • 16
  • 291
  • 322