如何知道 javafx 中 tableview 的动态创建按钮的行

How to know the row of a dynamic created button of a tableview on javafx

我目前正在研究 javafx,但遇到了一个严重的问题。我找不到一种方法来获取我在 table 视图上动态创建的按钮行的索引。

如果有人能帮助我,那将非常有帮助。

this.clmColumn.setCellFactory((TableColumn<?, ?> column) -> {
            return new TableCell<?, ?>() {
                @Override
                protected void updateItem(? item, boolean empty) {

                    super.updateItem(item, empty);
                    if (!empty) {
                        final HBox hbox = new HBox(5);
                        final VBox vbox = new VBox(5);
                        Label label = new Label(item.toString());
                        final Button btnMais = new Button("+");
                        btnMais.setMinSize(25, 25);
                        final TableCell<?, ?> c = this;
                        btnMais.setOnAction(new EventHandler<ActionEvent>() {
                            @Override
                            public void handle(ActionEvent event) {
                                // At this point i want to select the current ROW of the button that i pressed on the tableview.

                            }
                        });
                        final Button btnMenos = new Button("-");
                        btnMenos.setMinSize(25, 25);
                        btnMenos.setOnAction(new EventHandler<ActionEvent>() {
                            @Override
                            public void handle(ActionEvent event) {
                                if (getItem() > 1) {
                                    // At this point i want to select the current ROW of the button that i pressed on the tableview.

                                }
                            }
                        });
                        final Button btnRemover = new Button("Remover");
                        btnRemover.setFont(new Font(8));
                        btnRemover.setOnAction(new EventHandler<ActionEvent>() {
                            @Override
                            public void handle(ActionEvent event) {
                                // At this point i want to select the current ROW of the button that i pressed on the tableview.

                            }
                        });
                        vbox.getChildren().add(hbox);
                        vbox.getChildren().add(btnRemover);
                        hbox.getChildren().add(btnMais);
                        hbox.getChildren().add(label);
                        hbox.getChildren().add(btnMenos);
                        hbox.setAlignment(Pos.CENTER);
                        vbox.setAlignment(Pos.CENTER);
                        setGraphic(vbox);
                    } else {
                        setGraphic(null);
                    }

                }
            };
        });

handle()方法中你可以做到

Object row = getTableView.getItems().get(getIndex());

如果您在整个类型参数中使用更具体的类型,则可以将 Object 替换为更具体的类型。