删除项目/添加新项目后,JavaFX TableView控件未正确重新绘制

时间:2014-12-16 12:04:53

标签: javafx controls tableview

我使用ObservableList来控制项目,每次删除项目时,TableView会将其从数据源中删除。但视图未更新,因为我仍然看到所有项目。唯一的区别是无法再选择已删除的项目。

类似问题:javafx listview and treeview controls are not repainted correctly

以下代码:

final TableColumn<TMachineType, String> testerType = new TableColumn<TMachineType, String>(
            bundle.getString("table.testerType"));

    testerType
            .setCellValueFactory(new PropertyValueFactory<TMachineType, String>(
                    "testerType"));

    testerType
            .setCellFactory(new Callback<TableColumn<TMachineType, String>, TableCell<TMachineType, String>>() {

                @Override
                public TableCell<TMachineType, String> call(
                        final TableColumn<TMachineType, String> param) {
                    final TableCell<TMachineType, String> cell = new TableCell<TMachineType, String>() {
                        @Override
                        protected void updateItem(final String item,
                                final boolean empty) {
                            super.updateItem(item, empty);
                            textProperty().unbind();
                            if (empty || item == null) {
                                setGraphic(null);
                                setText(null);
                            }
                            if (!empty) {
                                    final TMachineType row = (TMachineType) getTableRow().getItem();
                                    textProperty().bind(
                                            row.testerTypeProperty());
                                }

                        }
                    };
                    return cell;
                }
            });

TMachineType类:

private final SimpleStringProperty testerType = new SimpleStringProperty();

    @Column(name = "TESTER_TYPE")
public String getTesterType() {
    return testerType.get();
}

public void setTesterType(String testerType) {
    this.testerType.set(testerType);
}

public StringProperty testerTypeProperty() {
    return testerType;
}

可观察名单:  1.数据库实体:

final Query q = em.createQuery("SELECT t FROM TMachineType t");
final List resultList = q.getResultList();

2。 OBS。列表设置:

    ObservableList<TMachineType> observableList;
...
    observableList = FXCollections.observableArrayList(resultList);
    tableMachineType.setItems(observableList);
    FXCollections.sort(observableList);
  1. 行删除:

    @FXML
    private void handleOnRemove(final ActionEvent event) {
    
    final ObservableList<TMachineType> selectedIndices = tableMachineType
            .getSelectionModel().getSelectedItems();
    final String infoTxt = selectedIndices.size() + " "
            + bundle.getString("message.records_removed");
    
    final List<TMachineType> deleteBuffer = new ArrayList<TMachineType>();
    deleteBuffer.addAll(selectedIndices);
    
    for (final TMachineType selectedIdx : deleteBuffer) {
    
        selectedIdx.manufacturerProperty().unbind();
        selectedIdx.testerTypeProperty().unbind();
    
        deleted.add(selectedIdx);
        observableList.remove(selectedIdx);
        // tableMachineType.getItems().remove(selectedIdx);
    
    }
    ..
    

    }

1 个答案:

答案 0 :(得分:0)

删除cellFactory解决了这个问题! YEHU:)

testerType
        .setCellFactory(new Callback<TableColumn<TMachineType, String>, TableCell<TMachineType, String>>() {

            @Override
            public TableCell<TMachineType, String> call(
                    final TableColumn<TMachineType, String> param) {
                final TableCell<TMachineType, String> cell = new TableCell<TMachineType, String>() {
                    @Override
                    protected void updateItem(final String item,
                            final boolean empty) {
                        super.updateItem(item, empty);
                        textProperty().unbind();
                        if (empty || item == null) {
                            setGraphic(null);
                            setText(null);
                        }
                        if (!empty) {
                                final TMachineType row = (TMachineType) getTableRow().getItem();
                                textProperty().bind(
                                        row.testerTypeProperty());
                            }

                    }
                };
                return cell;
            }
        });