1

Trying to add children to a GridPane with row and column indices. They show up in the gui, but using GridPane.getColumnIndex(node) always returns null.

GridPane grid = new GridPane();
for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 20; j++) {
        grid.setGridLinesVisible(true);

        Button myBtn = new Button("foo");

        GridPane.setColumnIndex(myButton,i);
        GridPane.setRowIndex(myButton,j);

        grid.getChildren().add(myButton);
    }

When I try to use this algorithm I got from the answer on javafx GridPane retrive specific Cell content, the program crashes with a NullPointerException.

private Node getNodeFromGridPane(GridPane gridPane, int col, int row)
{
    for (Node node : gridPane.getChildren()) {
        if (GridPane.getColumnIndex(node) == col && GridPane.getRowIndex(node) == row) {
            return node;
        }
    }
    return null;
}

Why are GridPane.getRowIndex() and GridPane.getColumnIndex() returning null even after calling setColumnIndex() and setRowIndex()?

Fubeca
  • 13
  • 4
  • 1
    In this code you create an instance of a `Button` named **myBtn**. Configure an instance with the name of a **myButton** and a pint put something called **myCell** in the panel. – mr mcwolf Dec 08 '17 at 06:54
  • Also without a closing `)` after the method parameters the `getNodeFromGridPane` method shouldn't even compile. – fabian Dec 08 '17 at 10:19

1 Answers1

1

So it seems I got it to work if you run it you will see that all the buttons are red white or blue this is because they were found

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


public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {

        GridPane grid = new GridPane();
        grid.setGridLinesVisible(true);

        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                grid.add(new Button("foo"),i,j);
                //                Button myBtn = new Button("foo");
                //                GridPane.setColumnIndex(myBtn, i);
                //                GridPane.setRowIndex(myBtn, j);
                //                grid.getChildren().add(myBtn);
            }
        }


        Scene scene = new Scene(grid);

        Stage stage = new Stage();
        stage.setScene(scene);
        stage.show();

        String[] colors = {"-fx-background-color: red;", "-fx-background-color: blue;", "-fx-background-color: white;"};
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                Node node = getNodeFromGridPane(grid,i,j);
                if(node instanceof Button)
                    node.setStyle(colors[(int) (Math.random() * 3)]);
            }
        }
    }

    private Node getNodeFromGridPane(GridPane gridPane, int col, int row) {
        for (Node node : gridPane.getChildren())
            if (GridPane.getColumnIndex(node) != null
                    && GridPane.getColumnIndex(node) != null
                    && GridPane.getRowIndex(node) == row
                    && GridPane.getColumnIndex(node) == col)
                return node;
        return null;
    }

    public static void main(String[] args) { launch(args); }
}
Matt
  • 3,052
  • 1
  • 17
  • 30