FXML视图
<TableView fx:id="productTable" layoutY="70.0" prefHeight="474.0" prefWidth="750.0">
<columns>
<TableColumn fx:id="numberColumn" prefWidth="54.0" resizable="false" text="N#"/>
<TableColumn prefWidth="235.0" resizable="false" text="%items.name.column">
<cellValueFactory>
<PropertyValueFactory property="name"/>
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="189.0" resizable="false" text="%items.barCode.column">
<cellValueFactory>
<PropertyValueFactory property="barcode"/>
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="73.0" resizable="false" text="%items.amount.column" /> // TODO: figure out how to count!
<TableColumn prefWidth="98.0" resizable="false" text="%items.pricePerItem.column">
<cellValueFactory>
<PropertyValueFactory property="price"/>
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="totalPriceCoulmn" prefWidth="100.0" resizable="false" text="%items.totalPrice.column"/>
</columns>
</TableView>
产品DTO
public class ProductDto {
@NotNull(message = "product.barcode.null")
@Size(max = 13, message = "product.barcode.max.length")
private final String barcode;
@NotNull(message = "product.name.null")
@Size(max = 256, message = "product.name.max.length")
private final String name;
@Min(value = 1, message = "product.price.min")
private final int price;
@JsonCreator
public ProductDto(@JsonProperty("barcode") String barcode,
@JsonProperty("name") String name,
@JsonProperty("price") int price) {
this.barcode = barcode;
this.name = name;
this.price = price;
}
public String getBarcode() {
return barcode;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
@Override
public boolean equals(Object o) {
if(this == o) return true;
if(o == null || getClass() != o.getClass()) return false;
ProductDto that = (ProductDto) o;
return Objects.equals(barcode, that.barcode);
}
@Override
public int hashCode() {
return Objects.hash(barcode);
}
@Override
public String toString() {
return "ProductDto{" +
"barcode='" + barcode + '\'' +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}
正如你可以看到dto没有字段数量,所以问题是:即使dto中没有字段,有没有办法增加列的值(类似于 PropertyValueFactory property =“? !“)?
控制器
/**
* @author Timur Berezhnoi.
*/
public class MainController {
@FXML
private TableView<ProductDto> productTable;
// The other code is omitted
}