GridPane.getColumnIndex()返回null [英] GridPane.getColumnIndex() returning null

查看:84
本文介绍了GridPane.getColumnIndex()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将子项添加到具有行和列索引的GridPane中.它们显示在gui中,但使用GridPane.getColumnIndex(node)始终返回null.

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

当我尝试使用此算法时,我从 javafx GridPane检索特定的答案中得到了答案单元格内容,程序会因NullPointerException而崩溃.

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

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

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

推荐答案

因此,如果您运行它,看来它可以正常工作,您会看到所有按钮都是红色,白色或蓝色,这是因为找到了它们

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

这篇关于GridPane.getColumnIndex()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆