Ayoub.A
Ayoub.A

Reputation: 2093

FXML: Bind to nested field

I have a TableColumn coded as:

<TableColumn text="Nom" prefWidth="${purchasesTable.width*0.65}">
    <cellValueFactory>
        <PropertyValueFactory property="item.name" />
    </cellValueFactory>
</TableColumn>

it's TableView's items property is bound to a list of Purchase class:

Purchase class:

public class Purchase {

private Item item;


public Item getItem() {
    return item;
}

public void setItem(Item item) {
    this.item = item;
}

}

My Item class is as follows:

public class Item {
private long id;
private StringProperty name = new SimpleStringProperty();
private DoubleProperty price = new SimpleDoubleProperty();

//Getters and Setters
public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}

public final StringProperty nameProperty() {
    return this.name;
}


public final String getName() {
    return this.nameProperty().get();
}


public final void setName(final String name) {
    this.nameProperty().set(name);
}

}

when I add Purchases to my table the name cell do not appear. What am I doing wrong? and is it necessary to my Item fields as properties, because I want to use them somewhere else where JavaFX is not used?

Upvotes: 1

Views: 496

Answers (1)

James_D
James_D

Reputation: 209330

PropertyValueFactory does not support "properties of properties". You need to implement the Callback yourself here, which has to be done in the controller class:

public class MyController {

    @FXML
    private TableColumn<Purchase, String> nameColumn ;

    public void initialize() {
        nameColumn.setCellValueFactory(cellData -> {
            String name ;
            Purchase purchase = cellData.getValue(); 
            if (purchase == null) {
                name = null ;
            } else {
                name = purchase.getName();
            }
            return new SimpleStringProperty(name);
        });
        // ...
    }

    // ...
}

and then in the fxml, of course, you need to map the table column to the field in the controller

<TableColumn fx:id="nameColumn" text="Nom" prefWidth="${purchasesTable.width*0.65}" />

Upvotes: 2

Related Questions