How do I change the text of a JavaFX label only via the ID? A simplified version of the code is shown below.
For context, we have a grid (GridPane) of labels (it's a sudoku game), the labels are numbered from Label_0_0, ..., to Label_8_8 (indicating the location in the grid). We are going over the label grid in a nested for loop and are trying to set different numbers at different lables/grid-boxes using the ID at the current coordinates of the for loop. (For simplicity, I omitted the for loop etc. in the code example.)
I've searched the entire internet for answers and haven't figured it out yet, any help is highly appreciated :)
String labelID = "ID_1";
int index = 5;
Label currentlySelectedLabel = (Label) ???; // sth like xx.getElementByID, if that existed
currentlySelectedLabel.setText(Integer.toString(index));
Some reference to the labels in the FXML:
...
<Label fx:id="Label_8_6" alignment="CENTER" onMouseClicked="#MyFrstLabelClicked" onMouseMoved="#OnMouseMovedOnLabel" prefHeight="44.0" prefWidth="46.0" text="0" GridPane.columnIndex="6" GridPane.rowIndex="8" />
<Label fx:id="Label_8_7" alignment="CENTER" onMouseClicked="#MyFrstLabelClicked" onMouseMoved="#OnMouseMovedOnLabel" prefHeight="44.0" prefWidth="46.0" text="0" GridPane.columnIndex="7" GridPane.rowIndex="8" />
<Label fx:id="Label_8_8" alignment="CENTER" onMouseClicked="#MyFrstLabelClicked" onMouseMoved="#OnMouseMovedOnLabel" prefHeight="44.0" prefWidth="46.0" text="0" GridPane.columnIndex="8" GridPane.rowIndex="8" />
</children>
</GridPane>
</center>
</BorderPane>