Java TableView中的“删除”按钮不消失

Java TableView中的“删除”按钮不消失,java,javafx,fxml,scenebuilder,Java,Javafx,Fxml,Scenebuilder,我遇到了一个小问题(其实没什么大不了的,但这让我很恼火),因为在我从表中删除了一条记录后,我还希望删除按钮取消显示,现在它的工作方式如下: 单击删除按钮之前: 单击第一条记录的删除按钮后: 另外,这是我用来处理此事件的函数: private class ButtonCell extends TableCell<Record, Boolean> { final Button cellButton = new Button("Delete"); ButtonCell

我遇到了一个小问题(其实没什么大不了的,但这让我很恼火),因为在我从表中删除了一条记录后,我还希望删除按钮取消显示,现在它的工作方式如下:

单击删除按钮之前:

单击第一条记录的删除按钮后

另外,这是我用来处理此事件的函数:

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

        }


    }
}
private class ButtonCell扩展了TableCell{
最终按钮cellButton=新按钮(“删除”);
ButtonCell(){
cellButton.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent t){
警报警报=新警报(警报.警报类型.确认);
alert.setTitle(“确认对话框”);
alert.setHeaderText(“删除整行?”);
可选结果=alert.showAndWait();
if(result.get()==ButtonType.OK){
Animal currentAnimal=(动物)ButtonCell.this.getTableView().getItems().get(ButtonCell.this.getIndex());
数据删除(当前动物);
cellButton.setVisible(假);
}
}
});
}
//如果行不为空,则显示按钮
@凌驾
受保护的void updateItem(布尔值t,布尔值为空){
super.updateItem(t,空);
如果(!空){
设置图形(cellButton);
}
}
}
试试这个:

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

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

应该有答案吗?应该有答案吗?非常感谢。工作完美:)谢谢!工作完美:)