helpplz
helpplz

Reputation: 23

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

I'm currently working on javafx and I have a serious problem. I can't find a way to get the index of the row of a button that I created dynamically on a table view.

If someone could help me out, that would be very helpful.

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);
                    }

                }
            };
        });

Upvotes: 0

Views: 349

Answers (1)

James_D
James_D

Reputation: 209684

In the handle() method you can do

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

You can replace Object with a more specific type if you use more specific types throughout in the type parameters.

Upvotes: 1

Related Questions