簡體   English   中英

Javafx:更新TableCell

[英]Javafx: update TableCell

我有一個TableView和定制MyTableCell extends CheckBoxTreeTableCell<MyRow, Boolean> ,在該小區被@Overridden所述updateItem方法:

@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));
        }
    });
  • data是一個ObservableList<MyRow>
  • 這只是一個示例,也是我代碼的簡化版本

當我更改comboBox中的值時,直到我滾動或單擊該單元格,表格的chekbox才會消失。 有一個“解決方案”可以調用table.refresh(); 但是當我只想刷新一個單元格時,我不想刷新整個表。 因此,我嘗試添加一些偵聽器來觸發updateItem,但每次嘗試均失敗。 您是否知道如何觸發一個單元格而不是整個表的更新機制?

綁定單元格的圖形,而不僅僅是設置它:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM