(转帖)Implementing custom JavaFx Bindings
博客专区 > Ellipse 的博客 > 博客详情
(转帖)Implementing custom JavaFx Bindings
Ellipse 发表于4年前
(转帖)Implementing custom JavaFx Bindings
  • 发表于 4年前
  • 阅读 36
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

Built in Bindings

Probably you know the JavaFx property binding. ( If not here is a tutorial ). There are several ways to create a binding. The easiest way is to use the  bind  or  bindBidirectional  methods of the property you want to bind. For more complex bindings you can use the  Bindings  class. It provide a lot of special bindings as static methods e.g. ones that calulate number values, concat string properties, bind ObservableLists, ObservableMaps etc., but nothing for  BigDecimal ...

Here we have certain excamples for that:

//Binding with the bind method of a property
StringProperty prop1 = new SimpleStringProperty();
StringProperty prop2 = new SimpleStringProperty();
prop1.bind(prop2);
 
prop2.set("Hello World");
Assert.assertEquals(prop1.get(), prop2.get());
 
 
//Binding with Bindings.add method
IntegerProperty int1 = new SimpleIntegerProperty();
IntegerProperty int2 = new SimpleIntegerProperty();
NumberBinding sum = Bindings.add(int1, int2);
 
int1.set(2);
int2.set(3);
Assert.assertEquals(5, sum.getValue().intValue());

Custom implemented Bindings

If the built in bindings do not fit your needs, JavaFx provides base classes, that you can use for custom implemented bindings. You find them in the package  javafx.beans.bindings  and all their names end with  Binding .

There are only two things to do in your subclass of one of the  Binding  classes:

  • Think about on which other properties your new binding is dependand of. Normally this are the properties you calculate your new value of. Use the bind method to add those properties to the dependencies of the new binding in the initializer block (as done in the examples below) or in the constructor. If the value of one of those changes, the binding value is newly calculated.

  • Implement the computeValue method, which returns the new value of the binding.

Normally you think of numbers to compute a value. But you can do this with every object, that can be created from other values, if you use the ObjectBinding class.  

Here are several examples for that:

//Simple custom IntegerBinding implementation
IntegerProperty intVal = new SimpleIntegerProperty();
IntegerBinding modulo2 = new IntegerBinding() {
    { bind(intVal);}
    protected int computeValue() {
        return intVal.get() % 2;
    }
};
intVal.set(3);
Assert.assertEquals(1, modulo2.get());
intVal.set(4);
Assert.assertEquals(0, modulo2.get());
 
 
//Custom ObjectBinding of BigDecimal with two dependencies
ObjectProperty<BigDecimal> price = new SimpleObjectProperty<>();
ObjectProperty<BigDecimal> amount = new SimpleObjectProperty<>();
ObjectBinding<BigDecimal> totalPrice = new ObjectBinding<BigDecimal>() {
    { bind(price,amount);}
    protected BigDecimal computeValue() {
        if (price.get() == null || amount.get() == null) return null;
        return price.get().multiply(amount.get());
    }
};
 
price.set(new BigDecimal("3.50"));
amount.set(new BigDecimal("2.0"));
Assert.assertEquals(new BigDecimal("7.000"), totalPrice.get());
 
 
//Custom ObjectBinding of Rectangle2D with three dependencies
ObjectProperty<Bounds> boundsInLocalProperty = new SimpleObjectProperty<>();
DoubleProperty widthProperty = new SimpleDoubleProperty();
DoubleProperty heightProperty = new SimpleDoubleProperty();
 
ObjectBinding<Rectangle2D> rectBinding = new ObjectBinding<Rectangle2D>() {
    {bind(boundsInLocalProperty, widthProperty, heightProperty);}
    protected Rectangle2D computeValue() {
        Bounds bounds = boundsInLocalProperty.get();
        return new Rectangle2D(bounds.getMinX(), bounds.getMinY(),
                               widthProperty.get(), heightProperty.get());
    }
};


共有 人打赏支持
粉丝 45
博文 61
码字总数 41290
×
Ellipse
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: