Bind ObjectProperty with FloatProperty inside to other 属性
Bind ObjectProperty with FloatProperty inside to other property
我有一个 Article
class,里面有一些 Properties
。
public class Article {
private IntegerProperty id;
private StringProperty name;
private StringProperty description;
private FloatProperty price;
public Article(int id, String name, String description, float price) {
this.id = new SimpleIntegerProperty(id);
this.name = new SimpleStringProperty(name);
this.description = new SimpleStringProperty(description);
this.price = new SimpleFloatProperty(price);
}
...
}
我有一个 Sale
class 引用了一篇文章。
public class Sale {
private IntegerProperty id;
private ObjectProperty<Article> article;
private FloatProperty discount;
private IntegerProperty quantity;
private FloatProperty total;
public Sale(int id, Article article, float discount, int quantity) {
this.id = new SimpleIntegerProperty(id);
this.article = new SimpleObjectProperty<>(article);
this.discount = new SimpleFloatProperty(discount);
this.quantity = new SimpleIntegerProperty(quantity);
this.checked = new SimpleBooleanProperty(false);
this.total = new SimpleFloatProperty();
this.total.bind(Bindings.multiply(Bindings.subtract(this.article.get().priceProperty(), Bindings.multiply(this.article.get().priceProperty(), this.discount)), this.quantity));
}
public void setArticle(Article article) {
this.article.set(article);
}
...
}
如何正确绑定总数 属性?现在,如果我这样做,总计不会在 tableView 上发生动态变化。
Sale sale = new Sale(0, article1, 0.1f, 2);
sale.setArticle(article2);
我想我应该参考文章属性而不是值,但我不知道该怎么做。
你可以这样做:
public Sale(int id, Article article, float discount, int quantity) {
this.id = new SimpleIntegerProperty(id);
this.article = new SimpleObjectProperty<>(article);
this.discount = new SimpleFloatProperty(discount);
this.quantity = new SimpleIntegerProperty(quantity);
this.checked = new SimpleBooleanProperty(false);
this.total = new SimpleFloatProperty();
FloatProperty articlePriceProperty = new SimpleFloatProperty();
if (this.article != null) {
articlePriceProperty.bind(article.priceProperty());
}
this.article.addListener((obs, oldArticle, newArticle) -> {
articlePriceProperty.unbind();
if (newArticle == null) {
articlePriceProperty.set(0);
} else {
articlePriceProperty.bind(newArticle.priceProperty());
}
});
this.total.bind(Bindings.multiply(Bindings.subtract(articlePriceProperty, Bindings.multiply(articlePriceProperty(), this.discount)), this.quantity));
}
您可以使用 Bindings.selectFloat
:
this.total.bind(Bindings.multiply(Bindings.selectFloat(this.article, "price")
.multiply(this.quantity),
Bindings.subtract(1d, this.discount)));
我有一个 Article
class,里面有一些 Properties
。
public class Article {
private IntegerProperty id;
private StringProperty name;
private StringProperty description;
private FloatProperty price;
public Article(int id, String name, String description, float price) {
this.id = new SimpleIntegerProperty(id);
this.name = new SimpleStringProperty(name);
this.description = new SimpleStringProperty(description);
this.price = new SimpleFloatProperty(price);
}
...
}
我有一个 Sale
class 引用了一篇文章。
public class Sale {
private IntegerProperty id;
private ObjectProperty<Article> article;
private FloatProperty discount;
private IntegerProperty quantity;
private FloatProperty total;
public Sale(int id, Article article, float discount, int quantity) {
this.id = new SimpleIntegerProperty(id);
this.article = new SimpleObjectProperty<>(article);
this.discount = new SimpleFloatProperty(discount);
this.quantity = new SimpleIntegerProperty(quantity);
this.checked = new SimpleBooleanProperty(false);
this.total = new SimpleFloatProperty();
this.total.bind(Bindings.multiply(Bindings.subtract(this.article.get().priceProperty(), Bindings.multiply(this.article.get().priceProperty(), this.discount)), this.quantity));
}
public void setArticle(Article article) {
this.article.set(article);
}
...
}
如何正确绑定总数 属性?现在,如果我这样做,总计不会在 tableView 上发生动态变化。
Sale sale = new Sale(0, article1, 0.1f, 2);
sale.setArticle(article2);
我想我应该参考文章属性而不是值,但我不知道该怎么做。
你可以这样做:
public Sale(int id, Article article, float discount, int quantity) {
this.id = new SimpleIntegerProperty(id);
this.article = new SimpleObjectProperty<>(article);
this.discount = new SimpleFloatProperty(discount);
this.quantity = new SimpleIntegerProperty(quantity);
this.checked = new SimpleBooleanProperty(false);
this.total = new SimpleFloatProperty();
FloatProperty articlePriceProperty = new SimpleFloatProperty();
if (this.article != null) {
articlePriceProperty.bind(article.priceProperty());
}
this.article.addListener((obs, oldArticle, newArticle) -> {
articlePriceProperty.unbind();
if (newArticle == null) {
articlePriceProperty.set(0);
} else {
articlePriceProperty.bind(newArticle.priceProperty());
}
});
this.total.bind(Bindings.multiply(Bindings.subtract(articlePriceProperty, Bindings.multiply(articlePriceProperty(), this.discount)), this.quantity));
}
您可以使用 Bindings.selectFloat
:
this.total.bind(Bindings.multiply(Bindings.selectFloat(this.article, "price")
.multiply(this.quantity),
Bindings.subtract(1d, this.discount)));