博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(转帖)Implementing custom JavaFx Bindings
阅读量:7054 次
发布时间:2019-06-28

本文共 3570 字,大约阅读时间需要 11 分钟。

hot3.png

Built in Bindings

Probably you know the JavaFx property binding. ( ). 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 propertyStringProperty prop1 = new SimpleStringProperty();StringProperty prop2 = new SimpleStringProperty();prop1.bind(prop2); prop2.set("Hello World");Assert.assertEquals(prop1.get(), prop2.get());  //Binding with Bindings.add methodIntegerProperty 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 implementationIntegerProperty 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 dependenciesObjectProperty
 price = new SimpleObjectProperty<>();ObjectProperty
 amount = new SimpleObjectProperty<>();ObjectBinding
 totalPrice = new ObjectBinding
() {    { 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 dependenciesObjectProperty
 boundsInLocalProperty = new SimpleObjectProperty<>();DoubleProperty widthProperty = new SimpleDoubleProperty();DoubleProperty heightProperty = new SimpleDoubleProperty(); ObjectBinding
 rectBinding = new ObjectBinding
() {    {bind(boundsInLocalProperty, widthProperty, heightProperty);}    protected Rectangle2D computeValue() {        Bounds bounds = boundsInLocalProperty.get();        return new Rectangle2D(bounds.getMinX(), bounds.getMinY(),                               widthProperty.get(), heightProperty.get());    }};

转载于:https://my.oschina.net/u/580483/blog/183330

你可能感兴趣的文章
vi 常用操作命令
查看>>
Twitter ratchet
查看>>
Oracle卸载
查看>>
vtigercrm6.0beta版本发布
查看>>
Codeigniter 笔记(1)
查看>>
escape()、encodeURI()、encodeURIComponent()区别详解
查看>>
ios 点击navbar 标题栏 tableview返回顶部
查看>>
spring-boot & zxing 搭建二维码服务
查看>>
动手实现MVC: 3. AOP实现准备篇动态代理
查看>>
vim
查看>>
在Linux下用LVS和Ipvsadm做Web负载均衡
查看>>
【原创】回到头部 jquery+css3
查看>>
我有一个好点子,我正好也是一个码农
查看>>
联表取数据
查看>>
web.xml详解
查看>>
刘硕琛_下一代企业安全管理
查看>>
备战网络工程师认证考试:历年真题合集
查看>>
xargs
查看>>
RelativeLayout相对布局
查看>>
一个基于Python 装饰器的缓存库——wrapcache
查看>>