Issue
I've added 9 buttons to my gridpane using the following:
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
int number = 3 * r + c;
Button button = new Button(String.valueOf(number));
gridPane.add(button, c, r);
}
}
I want to replace the text in these buttons when they are clicked, but how do I do that? There's no identificator when adding these buttons, and when I try to print all the children of the gridpane it just gives me the memory address.
Solution
you can set and anonymous event like
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
int number = 3 * r + c;
Button button = new Button(String.valueOf(number));
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
((Button)e.getSource()).setText("Accepted");
}
});
gridPane.add(button, c, r);
}
}
Answered By - g kexxt