简体   繁体   中英

getRowIndex always returning null

I set up the gridpane like so:

for (int row = 0; row < mainBoard.getRows(); row++){
    for (int column = 0; column < mainBoard.getColumns(); column++) {
        mainBoard.setStatusArray(row, column, 0);
        Pane pane = new Pane();
        GridPane.setRowIndex(pane, row);
        GridPane.setColumnIndex(pane, column);
        gridpane.getChildren().add(new Pane());
        System.out.println(GridPane.getRowIndex(pane));
    }
}

And try to access it like so:

ObservableList<Node> childrens = gridpane.getChildren();
    for (Node node : childrens) {
        if (node instanceof Pane
            && GridPane.getRowIndex(node) == 1        <------------- line of error
            && getColumnIndex(node) == 1) {
                // Do stuff
    }
}

It seems that GridPane.getRowIndex(node) Always returns null , thus causing a NullPointerException and I cant figure out for the life of me why.

A solution found here may be helpful: GridPane.getColumnIndex() returning null

The user was having the same problem and by changing how they added the elements to the gridpane, it worked.

The answer provided by user Matt is:

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); }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM