我试图从活动列表中检索一些项目,以便使用JavaFX进行显示,但同时,如果我单击项目的按钮,我也希望能够删除这些项目。如何区分单击哪个按钮以删除右行?我正在尝试将lambda表达式分配给按钮,但显然它在for循环中不起作用。关于如何解决此问题的任何建议?
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)));
}
答案 0 :(得分:1)
我相信你想要:
JFXButton button = new JFXButton("", new ImageView(path);
gridPane.add(button),2,i);
button.setOnMouseClicked(e -> deleteActivity(gridPane.getRowIndex(i)));