本文介绍了Javafx:更新TableCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TableView 和一个自定义 MyTableCell扩展CheckBoxTreeTableCell< MyRow,Boolean> ,在这个单元格中 @Overridden updateItem 方法:

I have a TableView and a custom MyTableCell extends CheckBoxTreeTableCell<MyRow, Boolean>, In this cell is @Overridden the updateItem method:

@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<字符串> 我有一些项目,当我更改该组合框的值时,我想根据所选值设置复选框的可见性。所以我有一个听众:

I have a ComboBox<String> where I have some items, and when I change the value of that combobox I want to set the visibility of the checkboxes depending on selected value. So I have a listener:

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




  • 数据 ObservableList< MyRow>

  • 这只是我的代码的一个示例和简化版本

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

    When I change the value in comboBox the table's the chekboxes don't disappear until I scroll or click on that cell. There is a "sollution" to call table.refresh(); but I don't want to refresh the whole table, when I want to refresh just one cell. So I tried to adding some listeners to trigger the updateItem, but I failed at every attempt. Do you have any idea how can I trigger the update mechanism for one cell, not for the whole table?

    推荐答案

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

    Bind the cell's graphic, instead of just setting it:

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

    这篇关于Javafx:更新TableCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 18:34