简体   繁体   中英

How to replace the text in the buttons of my gridpane

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM