首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GridPane.getColumnIndex()返回null

GridPane.getColumnIndex()返回null
EN

Stack Overflow用户
提问于 2017-12-08 06:46:29
回答 1查看 1.6K关注 0票数 1

尝试使用行和列索引将子元素添加到GridPane中。它们在gui中显示,但是使用GridPane.getColumnIndex(节点)总是返回null。

代码语言:javascript
复制
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);
    }

当我尝试使用这个算法时,我从javafx GridPane retrive specific Cell content上得到的答案,程序崩溃了一个NullPointerException。

代码语言:javascript
复制
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;
}

为什么GridPane.getRowIndex()和GridPane.getColumnIndex()即使在调用setColumnIndex()和setRowIndex()之后也返回null?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-08 14:53:10

所以看来我让它起作用了,如果你运行它,你会发现所有的按钮都是红、白或蓝的,这是因为它们被找到了。

代码语言:javascript
复制
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); }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47709082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档