BlackLabrador
BlackLabrador

Reputation: 3108

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

Upvotes: 0

Views: 60

Answers (1)

James_D
James_D

Reputation: 209684

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

Upvotes: 1

Related Questions