FXML:绑定到嵌套字段
FXML: Bind to nested field
我有一个 TableColumn
编码为:
<TableColumn text="Nom" prefWidth="${purchasesTable.width*0.65}">
<cellValueFactory>
<PropertyValueFactory property="item.name" />
</cellValueFactory>
</TableColumn>
它是 TableView
的项目 属性 绑定到 Purchase
的列表 class:
购买class:
public class Purchase {
private Item item;
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
}
我的Item
class如下:
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);
}
}
当我将购买添加到我的 table 时,名称单元格不会出现。我究竟做错了什么?我的 Item
字段是否有必要作为属性,因为我想在其他不使用 JavaFX 的地方使用它们?
PropertyValueFactory
不支持"properties of properties"。你需要在这里自己实现 Callback
,这必须在控制器中完成 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);
});
// ...
}
// ...
}
然后在fxml中,当然需要将table列映射到controller中的字段
<TableColumn fx:id="nameColumn" text="Nom" prefWidth="${purchasesTable.width*0.65}" />
我有一个 TableColumn
编码为:
<TableColumn text="Nom" prefWidth="${purchasesTable.width*0.65}">
<cellValueFactory>
<PropertyValueFactory property="item.name" />
</cellValueFactory>
</TableColumn>
它是 TableView
的项目 属性 绑定到 Purchase
的列表 class:
购买class:
public class Purchase {
private Item item;
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
}
我的Item
class如下:
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);
}
}
当我将购买添加到我的 table 时,名称单元格不会出现。我究竟做错了什么?我的 Item
字段是否有必要作为属性,因为我想在其他不使用 JavaFX 的地方使用它们?
PropertyValueFactory
不支持"properties of properties"。你需要在这里自己实现 Callback
,这必须在控制器中完成 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);
});
// ...
}
// ...
}
然后在fxml中,当然需要将table列映射到controller中的字段
<TableColumn fx:id="nameColumn" text="Nom" prefWidth="${purchasesTable.width*0.65}" />