删除行时,不会删除带有嵌入按钮的单元格

时间:2015-04-08 12:42:10

标签: javafx javafx-8

美好的一天。当我用一个按钮(Customized TableCell)删除一行时,该行被删除,但TableCell里面有一个按钮。我如何用行删除它?

删除行时会发生这种情况:

格式化列:

private void initializeTable() {
        id.setCellValueFactory(cellData -> cellData.getValue().getIdProperty());
        names.setCellValueFactory(cellData -> cellData.getValue().getNamesProperty());
        surnames.setCellValueFactory(cellData -> cellData.getValue().getSurnamesProperty());
        dni.setCellValueFactory(cellData -> cellData.getValue().getDniProperty());
        birthDate.setCellValueFactory(cellData -> cellData.getValue().getBirthDateProperty());
        address.setCellValueFactory(cellData -> cellData.getValue().getAddressProperty());
        createCellPhone();
        email.setCellValueFactory(cellData -> cellData.getValue().getEmailProperty());
        state.setCellValueFactory(cellData -> cellData.getValue().getActiveProperty());
    }
    private void attachTxtSearchListener() {
        txtSearch.textProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue.isEmpty()) {
                customerList.setAll(customerBak); // restore original cusomer list
            }
        });
    }
    private void createCellPhone() {
        // define a simple boolean cell value for the action column so that the
        // column will only be shown for non-empty rows.
        phones.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<CustomerTblModel, Boolean>, ObservableValue<Boolean>>() {
            @Override
            public ObservableValue<Boolean> call(
                    TableColumn.CellDataFeatures<CustomerTblModel, Boolean> features) {
                return new SimpleBooleanProperty(features.getValue() != null);
            }
        });
        // create a cell value factory with an add button for each row in the table.
        phones.setCellFactory(new Callback<TableColumn<CustomerTblModel, Boolean>, TableCell<CustomerTblModel, Boolean>>() {
            @Override
            public TableCell<CustomerTblModel, Boolean> call(
                    TableColumn<CustomerTblModel, Boolean> booleanTableColumn) {
                return new ButtonCell(THIS, Modality.APPLICATION_MODAL);
            }
        });
    }

和TableCell:

public class ButtonCell extends TableCell<CustomerTblModel, Boolean> {
    private final StackPane pane = new StackPane();
    private final Button btnPhones = new Button("Teléfonos");

    public ButtonCell(final MainController FATHER, final Modality MODALITY) {
        pane.setPadding(new Insets(3));
        btnPhones.getStyleClass().add("btn-new");
        pane.getChildren().add(btnPhones);
        btnPhones.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                StageUtil.loadCustomerPhones(FATHER, MODALITY).showAndWait();
            }
        });
    }
    @Override
    protected void updateItem(Boolean item, boolean empty) {
        super.updateItem(item, empty);
        if (!empty) {
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
            setGraphic(pane);
        }
    }
}

感谢您的时间。

1 个答案:

答案 0 :(得分:2)

更新项目时覆盖所有案例。尝试

@Override
protected void updateItem(Boolean item, boolean empty) {
    super.updateItem(item, empty);
    if (!empty) {
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        setGraphic(pane);
    }
    else {
        setText(null);
        setGraphic(null);
    }
}