0

I am trying to retrieve some items from an activity list in order to show them with JavaFX but at the same time I also want to be able to delete these items if I click on the item's button. How can I distinguish which button I clicked in order to delete the right row? I'm trying to assign to the button the lambda expression but apparently it doesn't work in a for loop. Any suggestion on how to fix this?

   gridPane = new GridPane();

    String path = "image.png";
    items = new ArrayList<>();

    items.add("Activity 1");
    items.add("Activity 2");
    items.add("Activity 3");

    for (int i=0; i<items.size(); i++) {
        gridPane.add(new Text(items.get(i)), 1, i);
        gridPane.add(new JFXButton("", new ImageView(path)),2,i);
        (JFXButton)gridPane.setOnMouseClicked(e -> deleteActivity(gridPane.getRowIndex(i)));
    }
Jasmine
  • 117
  • 10

1 Answers1

1

I asuume you wanted :

JFXButton button = new JFXButton("", new ImageView(path);   
gridPane.add(button),2,i);
button.setOnMouseClicked(e -> deleteActivity(gridPane.getRowIndex(i)));
c0der
  • 18,467
  • 6
  • 33
  • 65