Search code examples
javajavafxcheckboxgridpane

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

  • 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.