'How do I see if a specific CheckBox is selected within a GridPane?

I've created a 10x10 GridPane of CheckBoxes. I need to see whether a specific CheckBox is selected, but the GridPane is made up of nodes. So If I access a particular node using a function from another thread, I can't use isSelected because it is the wrong type.

I've tried modifying the function getNodeByRowColumnIndex or forcing the type to be CheckBox but I'm not sure how.

@FXML
private GridPane Grid;

@FXML
public void initialize() {
    for (int x = 0; x < 10; x++) {
        for (int y = 0; y < 10; y++) {
            this.Grid.add(new CheckBox(), x, y);
            //Problem here
            boolean bln = getNodeByRowColumnIndex(y,x,this.Grid).isSelected();
        }
    }
}


Solution 1:[1]

getNodeByRowColumnIndex returns a Node. You need to cast it to CheckBox :

Node node = getNodeByRowColumnIndex(y,x,this.Grid);
    if(node instanceof CheckBox){
          boolean bln = ((CheckBox)node).isSelected();
          //todo use bln
}

Side note 1 : It is not clear why you want to check isSelected for a CheckBox you just added.
Side note 2: as per java naming conventions use GridPane grid.

Solution 2:[2]

Please refer to the source code below.

@FXML private GridPane imageGridPane;

imageGridPane.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler() {

@Override
public void handle(MouseEvent e) {
    Node clickedNode = (Node) e.getTarget();
    if (clickedNode != imageGridPane) {
        Node parent = clickedNode.getParent();
        while (parent != imageGridPane) {
            clickedNode = parent;
            parent = clickedNode.getParent();
        }
        Integer colIndex = GridPane.getColumnIndex(clickedNode);
        Integer rowIndex = GridPane.getRowIndex(clickedNode);
        System.out.println("Mouse clicked cell: " + colIndex + " And: " + rowIndex);
        CheckBox itemNoCheckBox = (CheckBox) ((BorderPane) clickedNode).getTop();
        System.out.println("itemNo: " + itemNoCheckBox.getText());
        if (itemNoCheckBox.isSelected()) {
            System.out.println("Selected");
        } else {
            System.out.println("Deselected");
        }
    }
}

});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2