【问题标题】:JavaFX: How to make a Table Cell to be contigent to the value of another Table CellJavaFX:如何使表格单元格取决于另一个表格单元格的值
【发布时间】:2015-07-02 09:29:49
【问题描述】:

我有一个由 3 个StringProperty 组成的非常简单的模型,我们称它们为property1property2property3。我在我的模型中定义了常用的get/set/property 方法。我现在使用TableView 来显示其中一些属性。 Column模型定义如下

TableColumn<MyModel, String> tableColumn1 = new TableColumn<MyModel, String>("display 1");
TableColumn<MyModel, String> tableColumn2 = new TableColumn<MyModel, String>("display 2");

现在 TableColumn 遵循使用链接到我模型的属性之一的单元格值工厂的模式。

tableColumn1.setCellValueFactory((t)-> {
                MyModel myModelValue  = ((MyModel)t.getValue());    
                return myModelValue.getProperty3().equals("something") ? property1():property2();
            });

tableColumn2.setCellValueFactory((t)-> property3());

现在的问题如下。如果property3 在执行过程中某处发生更改,它将正确触发我的表的column2 的更改和单元格的UI 更新。但它不会为 column1 做任何事情,因为 property1property2 都没有改变。我怎样才能以某种方式强制 column1 更改或视情况听 property3 的更改

谢谢

【问题讨论】:

    标签: java uitableview javafx tableview javafx-8


    【解决方案1】:
    tableColumn1.setCellValueFactory(t -> {
        MyModel myModelValue = t.getValue();
        return Bindings.when(myModelValue.property3().equals("something"))
            .then(myModelValue.property1())
            .otherwise(myModelValue.property2());
    });
    

    【讨论】:

    • 好吧,你不得不承认java已经变得非常整洁了!谢谢,漂亮的答案,我忘记了这个when绑定功能
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    相关资源
    最近更新 更多