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)));
    }
6
  • What's the exact error you get? Commented Mar 10, 2019 at 11:43
  • Variable used in lambda expression should be final or effectively final referred to the "i" in the last row Commented Mar 10, 2019 at 11:46
  • Possible duplicate of Variable used in lambda expression should be final or effectively final Commented Mar 10, 2019 at 11:50
  • Define a variable int ii = i; inside the loop, and use that in the lambda instead. Commented Mar 10, 2019 at 11:52
  • @AndyTurner now I get "Inconvertible types; cannot cast 'void' to 'com.jfoenix.controls.JFXButton'" because of the JFXButton casting Commented Mar 10, 2019 at 12:01

1 Answer 1

1

I asuume you wanted :

JFXButton button = new JFXButton("", new ImageView(path);   
gridPane.add(button),2,i);
button.setOnMouseClicked(e -> deleteActivity(gridPane.getRowIndex(i)));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.