在this question我被展示瞭如何通過改變它的包裝對象來處理改變屬性的問題,從而不發送改變的更新。溶液使用ReactFX:如何使用ReactFX的Var進行自定義綁定?
class Cell {
private final ObjectProperty<Shape> shape = new SimpleObjectProperty<>(new Shape());
// all getters and setterts
public static class Shape {
private final IntegerProperty size = new SimpleIntegerProperty(0);
// all getters and setterts
}
public static void main(String[] args) {
Var<Number> sizeVar = Val.selectVar(cell.shapeProperty(), Shape::sizeProperty);
sizeVar.addListener(
(obs, oldSize, newSize) -> System.out.println("Size changed from "+oldSize+" to "+newSize));
}
所以,現在如果shape
財產本身的變化就觸發size
變化太大(除非新的形狀大小相同)。但是現在我想用自定義綁定綁定到屬性,並且我在下面解釋了一個問題。
我的數據類是這些:
class Cell {
private final ObjectProperty<Shape> shape = new SimpleObjectProperty<>();
public final ObjectProperty<Shape> shapeProperty() { return shape; }
public final Shape getShape() { return shapeProperty().get(); }
public final void setShape(Shape shape) { shapeProperty().set(shape); }
// other properties
}
class Shape {
private final IntegerProperty size = new SimpleIntegerProperty();
public final IntegerProperty sizeProperty() { return size; }
public final int getSize() { return size.get(); }
public final void setSize(int size) { sizeProperty().set(size); }
// other properties
}
,我想通過自己的特性結合GUI性質爲他們創造一個GUI表示。我做這種方式:
class CellRepresentation extends Group {
private final Cell cell;
CellRepresentation(Cell cell) {
this.cell = cell;
getChildren().add(new ShapeRepresentation() /*, other representations of things in the cell*/);
}
private class ShapeRepresentation extends Cylinder {
ObjectProperty<Shape> shape;
private ShapeRepresentation() {
super(100, 100);
shape = new SimpleObjectProperty<Shape>(cell.getShape());
shape.bind(cell.shapeProperty());
Var<Number> sizeVar = Val.selectVar(cell.shapeProperty(), Shape::sizeProperty);
// THIS WILL WORK
materialProperty().bind(Bindings.createObjectBinding(() -> {
if (shape.get() == null)
return new PhongMaterial(Color.TRANSPARENT);
return new PhongMaterial(Color.RED);
}, sizeVar));
// THIS WILL NOT WORK
materialProperty().bind(sizeVar.map(n -> {
if (shape.get() == null)
return new PhongMaterial(Color.TRANSPARENT);
return new PhongMaterial(Color.RED);
}));
}
}
// the other representations of things in the cell
}
當我運行的結合將創造一個透明圓柱體的第一個選項下面的代碼。第二個選項將創建一個白色(默認顏色)圓柱體。我不知道爲什麼會發生這種情況。
public class Example extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Cell cell = new Cell();
CellRepresentation cellRep = new CellRepresentation(cell);
Group group = new Group(cellRep);
Scene scene = new Scene(group, 200, 200, Color.AQUA);
stage.setScene(scene);
stage.show();
}
}
如果這不是一種使用綁定爲數據類創建表示的好方法,我也可以提供設計建議。
什麼是'在你最後的代碼塊size' ?難道你不需要像if(sizeVar.getValue()。intValue()== 1){...}等等嗎? –
@James_D'size'是'Shape'的屬性。我之前用你寫的這行代碼嘗試過(從'sizeVar'中取值),但沒有區別。我將創建一個MCVE。我認爲這只是錯誤的用法。 – Mark
是的,我很困惑。當然'size'是'Shape'的成員,但綁定不在Shape類中。而且這無法編譯,因爲'size'是一個'IntegerProperty',不能用'=='與'1'進行比較。如果該版本不起作用,綁定可能會被垃圾收集。我的答案不起作用嗎? –