FXML: Bind to nested field

こ雲淡風輕ζ 提交于 2019-12-25 09:17:10

问题


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?


回答1:


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}" />


来源:https://stackoverflow.com/questions/41317083/fxml-bind-to-nested-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!