简体   繁体   中英

JavaFX: How to make a Table Cell to be contigent to the value of another Table Cell

I have a very simple model composed of 3 StringProperty , let's call them property1 , property2 and property3 . I have defined the usual get/set/property methods in my model. I'm now using a TableView to display some of these properties. The Column model is defined as follow

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

now the TableColumn follow the pattern of using a cell value factory linked to one of the property of my model.

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

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

The problem is now the following. If property3 is changed somewhere during the execution, it will correctly trigger a change in the column2 of my table and an UI update of the cell. But it won't do anything for column1 as neither property1 and property2 have changed. How can I somehow force the column1 to change or listen contingently to the change of property3

thanks

tableColumn1.setCellValueFactory(t -> {
    MyModel myModelValue = t.getValue();
    return Bindings.when(myModelValue.property3().equals("something"))
        .then(myModelValue.property1())
        .otherwise(myModelValue.property2());
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM