Search code examples
javajavafxjavafx-8fxml

JavaFX: Way to change value of programmatically created buttons


I'm trying to change the value of buttons which are created in a for loop. The value of the buttons must be saved in a hashmap which contains the id of the button and the value.

This is what I currently have:

private void createMap(int blocksX, int blocksY) {
        // blocksX and blocksY are the amount of buttons to be placed
        for (int x = 0; x < blocksX; x++) {
            for (int y = 0; y < blocksY; y++) {
                Button btn = new Button();
                btn.setText("0");
                btn.setPrefSize(32, 32);
                btn.setLayoutX(32 * x);
                btn.setLayoutY(32 * y);
                btn.setId(String.valueOf(button_id));
                map_list.put(button_id, 0);
                button_id+=1;
                items.getChildren().addAll(btn);
                // If the user clicks a button, change the value of it...
                btn.setOnAction(click -> {
                    if(btn.getText() == "0"){
                        changeButtonValue(Integer.parseInt(btn.getId()), 1);
                        btn.setText("1");
                    } else if(btn.getText() == "1") {
                        changeButtonValue(Integer.parseInt(btn.getId()), 0);
                        btn.setText("0");
                    }
                });
            }
        }
    }

But now the only item in the HashMap to be updated is the last created button. How can I change this so it will update all button values?


Solution

  • I completed the code to a runnable example. And I can't see your problem.

    import java.util.HashMap;
    import java.util.Map;
    
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.AnchorPane;
    import javafx.stage.Stage;
    
    public class FX01 extends Application {
        public int button_id = 0;
        public Map<Integer, Integer> map_list = new HashMap<>();
        public AnchorPane items;
    
        public static void main(String[] args) {
            launch(args);
    
        }
    
        @Override
        public void start(Stage stage) throws Exception {
            items = new AnchorPane();
            createMap(5, 5);
    
            Scene scene = new Scene(items);
            stage.setScene(scene);
    
            stage.show();
        }
    
    
        private void createMap(int blocksX, int blocksY) {
            // blocksX and blocksY are the amount of buttons to be placed
            for (int x = 0; x < blocksX; x++) {
                for (int y = 0; y < blocksY; y++) {
                    Button btn = new Button();
                    btn.setText("0");
                    btn.setPrefSize(32, 32);
                    btn.setLayoutX(32 * x);
                    btn.setLayoutY(32 * y);
                    btn.setId(String.valueOf(button_id));
                    map_list.put(button_id, 0);
                    button_id+=1;
                    items.getChildren().addAll(btn);
                    // If the user clicks a button, change the value of it...
                    btn.setOnAction(click -> {
                        if(btn.getText() == "0"){
                            changeButtonValue(Integer.parseInt(btn.getId()), 1);
                            btn.setText("1");
                        } else if(btn.getText() == "1") {
                            changeButtonValue(Integer.parseInt(btn.getId()), 0);
                            btn.setText("0");
                        }
                    });
                }
            }
        }
    
        private void changeButtonValue(int id, int value) {
            map_list.put(id, value);
            System.out.println("map_list: " + map_list);
        }
    }