Suraj Dangat
Suraj Dangat

Reputation: 79

Adding same action to all buttons but action not applied to last button in Javafx

I have FirstFxml.fxml file and its controller as FirstFxmlController. I need to create custom run time buttons depending upon the sizeOfList(Currently assuming it as 4). I created following code and added action for it. But it cant apply this action to last button. Means by clicking on last button(i.e. Click Me 4), setOnAction not working for it. I tried this with lambda and handler but same issue with them also. Is there any javafx issue for following code, suggest me changes for this issue.

public class FirstFxmlController implements Initializable{

@FXML
public VBox centerVBox;

@Override
public void initialize(URL location, ResourceBundle resources) {
    //Adding multiple buttons
    int sizeOfList = 5;
    StackPane stack[] = new StackPane[sizeOfList];
    Button b[] = new Button[sizeOfList];

    for(int i = 1; i<sizeOfList; i++){
            b[i] = new Button("Click me "+i);

        b[i].setOnAction(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent e) {
                System.out.println("Clicked");
            }
        });


        stack[i] = new StackPane();
        stack[i].setMargin(b[i], new Insets(20, 0, 0, 0));
        stack[i].getChildren().add(b[i]);
        centerVBox.getChildren().add(stack[i]);
    }

}

Upvotes: 0

Views: 191

Answers (1)

Suraj Dangat
Suraj Dangat

Reputation: 79

Sorry it was my mistake, as vbox is in center part of border pane and size for this border pane was fixed. So what happens is bottom element of borderpane overrides on last button. So it causes not functioning of this last button. Problem solved. I just increase the size of main screen. Thanks buddies for your instant replay and suggestions.

Upvotes: 1

Related Questions