Siddhant Rangari
Siddhant Rangari

Reputation: 19

How can I add GridPane(contains 3 Buttons) to the Cell of a JAvaFX TableView

I have a TableView named as "customers" with TableColumns related to customer details. In the same TableView, I want to add one more TableColum as "Action" in which User should have possibilities to Add,View and delete Product details for particular Customer.

Tried multiple things by googling it but till now didn't get any solution for it.

Upvotes: 0

Views: 820

Answers (1)

fabian
fabian

Reputation: 82491

Just add a TableColumn with a custom cellFactory. BTW: You probably want to use a single column/row for the Buttons, in which case you can simply use HBox or VBox, but you could of course replace the layout in the following code:

TableColumn<Product, Void> buttonColumn = new TableColumn<>("Action");
buttonColumn.setCellFactory(col -> new TableCell<Product, Void>() {

    private final VBox container;

    {
        Button add = new Button("Add");
        Button view = new Button("View");
        Button delete = new Button("Delete");

        delete.setOnAction(evt -> {
            // delete this row item from TableView items
            getTableView().getItems().remove(getIndex());
        });

        view.setOnAction(evt -> {
            // call some method with the row item as parameter
            viewProduct(getTableRow().getItem());
        });

        add.setOnAction(...);

        container = new VBox(5, add, view, delete);
    }

    @Override
    public void updateItem(Void item, boolean empty) {
        super.updateItem(item, empty);
        setGraphic(empty ? null : container);
    }
});

tableView.getColumns().add(buttonColumn);

Upvotes: 1

Related Questions