Vinci
Vinci

Reputation: 341

Delete button in TableView does not dissapear

I've got a little problem (it's really not that big of a deal but it annoys me), because after I delete a record from my table, I also want my delete button to disapear, right now it works like this:

BEFORE clicking the DELETE button:

enter image description here

AFTER clicking first record's DELETE button:

enter image description here

Also, this is the function that I'm using to handle this event:

private class ButtonCell extends TableCell<Record, Boolean> {
    final Button cellButton = new Button("Delete");

    ButtonCell(){

        cellButton.setOnAction(new EventHandler<ActionEvent>(){

            @Override
            public void handle(ActionEvent t){

                Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
                alert.setTitle("Confirmation Dialog");
                alert.setHeaderText("Delete the entire row?");

                Optional<ButtonType> result = alert.showAndWait();
                if (result.get() == ButtonType.OK){
                    Animal currentAnimal = (Animal) ButtonCell.this.getTableView().getItems().get(ButtonCell.this.getIndex());
                    data.remove(currentAnimal);
                    cellButton.setVisible(false);
                }
            }
        });

    }
    //Display button if the row is not empty
    @Override
    protected void updateItem(Boolean t, boolean empty) {
        super.updateItem(t, empty);
        if(!empty){
            setGraphic(cellButton);

        }


    }
}

Upvotes: 0

Views: 163

Answers (1)

monolith52
monolith52

Reputation: 884

Try this:

@Override
protected void updateItem(Boolean t, boolean empty) {
    super.updateItem(t, empty);
    if(empty){
        setGraphic(null);
    } else {
        setGraphic(cellButton);
    }
}

Upvotes: 2

Related Questions