JavaFX-将ObjectProperty绑定到另一个ObjectProperty中的成员?
我有一个叫做JavaFX-将ObjectProperty绑定到另一个ObjectProperty中的成员?,java,javafx,properties,binding,Java,Javafx,Properties,Binding,我有一个叫做Tree public class Tree { private Color c; public Color getColor() { return c; } } 我有一个ObjectProperty ObjectProperty<Tree> importantTree = new SimpleObjectProperty(); 只需使用绑定: ObjectProperty<Tree> impo
Tree
public class Tree
{
private Color c;
public Color getColor()
{
return c;
}
}
我有一个ObjectProperty
ObjectProperty<Tree> importantTree = new SimpleObjectProperty();
只需使用绑定:
ObjectProperty<Tree> importantTree = new SimpleObjectProperty();
Binding<Color> importantTreesColor = Bindings.createObjectBinding(() ->
importantTree.get() == null ? null : importantTree.get().getColor(),
importantTree);
Tree a = new Tree(Color.RED);
Tree b = new Tree(Color.GREEN);
importantTree.set(a);
System.out.println(importantTreesColor.getValue()); // Prints RED.
importantTree.set(b);
System.out.println(importantTreesColor.getValue()); // Prints GREEN.
ObjectProperty ImportTree=新的SimpleObject属性();
Binding importantTreesColor=Bindings.createObjectBinding(()->
importantTree.get()==null?null:importantTree.get().getColor(),
重要树);
树a=新树(颜色为红色);
树b=新树(颜色为绿色);
重要树集(a);
System.out.println(importantTreesColor.getValue());//打印红色。
重要树集(b);
System.out.println(importantTreesColor.getValue());//打印绿色。
你也可以
Binding<Color> importantTreesColor = new ObjectBinding<Color>() {
{ bind(importantTree); }
@Override
protected Color computeValue() {
return importantTree.get()==null ? null : importantTree.get().getColor();
}
};
Binding importantTreesColor=new ObjectBinding(){
{bind(importantTree);}
@凌驾
受保护的颜色计算值(){
返回importantTree.get()==null?null:importantTree.get().getColor();
}
};
如果您愿意。只需使用绑定:
ObjectProperty<Tree> importantTree = new SimpleObjectProperty();
Binding<Color> importantTreesColor = Bindings.createObjectBinding(() ->
importantTree.get() == null ? null : importantTree.get().getColor(),
importantTree);
Tree a = new Tree(Color.RED);
Tree b = new Tree(Color.GREEN);
importantTree.set(a);
System.out.println(importantTreesColor.getValue()); // Prints RED.
importantTree.set(b);
System.out.println(importantTreesColor.getValue()); // Prints GREEN.
ObjectProperty ImportTree=新的SimpleObject属性();
Binding importantTreesColor=Bindings.createObjectBinding(()->
importantTree.get()==null?null:importantTree.get().getColor(),
重要树);
树a=新树(颜色为红色);
树b=新树(颜色为绿色);
重要树集(a);
System.out.println(importantTreesColor.getValue());//打印红色。
重要树集(b);
System.out.println(importantTreesColor.getValue());//打印绿色。
你也可以
Binding<Color> importantTreesColor = new ObjectBinding<Color>() {
{ bind(importantTree); }
@Override
protected Color computeValue() {
return importantTree.get()==null ? null : importantTree.get().getColor();
}
};
Binding importantTreesColor=new ObjectBinding(){
{bind(importantTree);}
@凌驾
受保护的颜色计算值(){
返回importantTree.get()==null?null:importantTree.get().getColor();
}
};
如果愿意的话。绑定和属性之间有什么区别?通常可以设置属性,大多数实现都包含实际值。绑定只是绑定到一个或多个其他可观察值的东西。去看相关的或相关的。太棒了,我理解。像往常一样,你真了不起。我希望我能吸收您所知道的关于JavaFX的所有信息,哈哈,绑定和属性之间的区别是什么?一般来说,属性是可以设置的,大多数实现都包含实际值。绑定只是绑定到一个或多个其他可观察值的东西。去看相关的或相关的。太棒了,我理解。像往常一样,你真了不起。我希望我能吸收你所知道的关于JavaFX的所有信息,哈哈