Javafx:更新TableCell

时间:2017-06-22 09:47:29

标签: java javafx tableview javafx-8

我有一个TableView和一个自定义MyTableCell extends CheckBoxTreeTableCell<MyRow, Boolean>,在此单元格中@OverriddenupdateItem方法:

@Override
public void updateItem(Boolean item, boolean empty) {
    super.updateItem(item, empty);
    if(!empty){
        MyRow currentRow = geTableRow().getItem();
        Boolean available = currentRow.isAvailable();
        if (!available) {
            setGraphic(null);
        }else{
            setGraphic(super.getGraphic())
        }
    } else {
        setText(null);
        setGraphic(null);
    }
}

我有ComboBox<String>我有一些项目,当我更改该组合框的值时,我想根据所选值设置复选框的可见性。所以我有一个倾听者:

comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue.equals("A") || newValue.equals("S")) {
            data.stream().filter(row -> row.getName().startsWith(newValue)).forEach(row -> row.setAvailable(false));
        }
    });
  • dataObservableList<MyRow>
  • 这只是我的代码的一个示例和简化版本

当我更改comboBox中的值时,表格中的chekbox不会消失,直到我滚动或点击该单元格为止。有一个&#34; sollution&#34;要调用table.refresh();,但我不想刷新整个表格,当我想刷新一个单元格时。所以我尝试添加一些侦听器来触发updateItem,但是每次尝试都失败了。您是否知道如何触发一个单元格的更新机制,而不是整个表格?

1 个答案:

答案 0 :(得分:1)

绑定单元格的图形,而不是仅仅设置它:

private Binding<Node> graphicBinding ;

@Override
protected void updateItem(Boolean item, boolean empty) {
    graphicProperty().unbind();
    super.updateItem(item, empty) ;

    MyRow currentRow = getTableRow().getItem();

    if (empty) {
        graphicBinding = null ;
        setGraphic(null);
    } else {
        graphicBinding = Bindings
            .when(currentRow.availableProperty())
            .then(super.getGraphic())
            .otherwise((Node)null);
        graphicProperty.bind(graphicBinding);
    }
}
相关问题