属性的属性有意义吗?

属性的属性有意义吗?,第1张

属性属性有意义吗?

仅使用标准JavaFX
API,您就可以利用这些

Bindings.selectXXX
方法来观察“属性”。

因此,例如:

import javafx.beans.binding.Bindings;import javafx.beans.property.IntegerProperty;import javafx.beans.property.ObjectProperty;import javafx.beans.property.SimpleIntegerProperty;import javafx.beans.property.SimpleObjectProperty;import javafx.scene.paint.Color;public class Cell {    private final ObjectProperty<Shape> shape = new SimpleObjectProperty<>(new Shape());    public final ObjectProperty<Shape> shapeProperty() {        return this.shape;    }    public final Cell.Shape getShape() {        return this.shapeProperty().get();    }    public final void setShape(final Cell.Shape shape) {        this.shapeProperty().set(shape);    }    public static class Shape {        private final IntegerProperty size = new SimpleIntegerProperty(0);        private final ObjectProperty<Color> color = new SimpleObjectProperty<>(Color.BLACK);        public final IntegerProperty sizeProperty() { return this.size;        }        public final int getSize() { return this.sizeProperty().get();        }        public final void setSize(final int size) { this.sizeProperty().set(size);        }        public final ObjectProperty<Color> colorProperty() { return this.color;        }        public final javafx.scene.paint.Color getColor() { return this.colorProperty().get();        }        public final void setColor(final javafx.scene.paint.Color color) { this.colorProperty().set(color);        }    }    public static void main(String[] args) {        Cell cell = new Cell();        Bindings.selectInteger(cell.shapeProperty(), "size").addListener(     (obs, oldSize, newSize) -> System.out.println("Size changed from "+oldSize+" to "+newSize));        cell.getShape().setSize(10);        cell.setShape(new Shape());        Shape s = new Shape();        s.setSize(20);        cell.setShape(s);    }}

将产生(期望的)输出

Size changed from 0 to 10Size changed from 10 to 0Size changed from 0 to 20

该API有点旧感,因为它依赖于将属性名称作为字符串传递,因此不是类型安全的,并且不能在编译时进行检查。此外,如果任何中间属性为null(例如,

cel.getShape()
在此示例中返回null),则绑定会生成烦人且冗长的警告消息(即使这被认为是受支持的用例)。

Tomas
Mikula在他的ReactFX库中有一个更现代的实现,请参阅此帖子以获取描述。使用ReactFX,您将执行以下 *** 作:

public static void main(String[] args) {    Cell cell = new Cell();    Var<Number> size = Val.selectVar(cell.shapeProperty(), Shape::sizeProperty);    size.addListener( (obs, oldSize, newSize) -> System.out.println("Size changed from "+oldSize+" to "+newSize));    cell.getShape().setSize(10);    cell.setShape(new Shape());    Shape s = new Shape();    s.setSize(20);    cell.setShape(s);}

最后,如果要创建单元格列表,则可以创建一个

ObservableList
指定的
extractor
。提取器是将列表中的每个元素(每个
Cell
)映射到
Observable
s
数组的函数。如果这些
Observable
更改中有任何更改,列表将触发更新事件。所以你可以做

ObservableList<Cell> cellList =     FXCollections.observableArrayList(cell -> new Observable[] {Bindings.selectInteger(cell.shapeProperty(), "size")});

使用标准API,或者

ObservableList<Cell> cellList =     FXCollections.observableArrayList(cell -> new Observable[] {Val.selectVar(cell.shapeProperty(), Shape::sizeProperty)});

使用ReactFX。然后,只需将a添加

ListChangeListener
到列表中,大小会发生变化(或形状更改为其他大小的新形状)时,系统会通知它。您可以根据需要在返回数组中添加尽可能多的可观察对象,这些可观察对象是单元格的属性。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5165541.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存