javafx adding button to grid pane


Question

I am adding button to grid pane dynamically but after giving them function they all show same function and i don't knows why?

import java.awt.Panel;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;

public class TestController implements Initializable {
    @FXML
    Panel mpanel;
    @FXML 
     GridPane gpnael;
int x=0,y=0,i=0,y1=0;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

    @FXML
    private void add(ActionEvent event) {
        y1++;
        Button  temp = new Button("Button " + i);
                temp.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent e) {
                        System.out.println("button"+y1);
                    }
                });

                gpnael.add(temp,i++,1);

    }

}

now i have added three button to grid pane when i click on each button they show same output.

I want that they all show different output as assigned .

1
1
7/12/2014 6:12:22 PM

Accepted Answer

You are not defining it in the buttons, you are always using a non final int to express your values you should try do make them with unique values or to set an id for each button and get the value from the id:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class ButtonsOnGPanel extends Application {

    private int i = 0;
    private GridPane gpnael = new GridPane();
    @Override
    public void start(Stage stage) throws Exception {
        Pane root = new Pane();
        while(i<3){
            addButton();
        }
        root.getChildren().add(gpnael);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
    private void addButton() {
        i++;
        final Button temp = new Button("Button " + i);
        final int numButton= i;
        temp.setId("" + i);
        temp.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                System.out.println("id(" + temp.getId()  + ") =  " + numButton);
            }
        });
        gpnael.add(temp, i, 1);
    }
    public static void main(String[] args) {
        launch(args);
    }
}

And if you want to use lambda expressions:

    temp.setOnAction((ActionEvent e) -> {
        System.out.println("id(" + temp.getId()  + ") =  " + numButton);
    });
4
7/13/2014 5:47:14 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon