java 如何绑定逆布尔值,JavaFX
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29616246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to bind inverse boolean, JavaFX
提问by Tomasz Mularczyk
My goal is to bind these two properties such as when checkbox
is selected then paneWithControls
is enabled and vice-versa.
我的目标是绑定这两个属性,例如何时checkbox
选择然后paneWithControls
启用,反之亦然。
CheckBox checkbox = new CheckBox("click me");
Pane paneWithControls = new Pane();
checkbox.selectedProperty().bindBidirectional(paneWithControls.disableProperty());
with this code however its opposite to what I want. I need something like inverse boolean binding. Is it possible or do I have to make a method to deal with it?
使用此代码但是它与我想要的相反。我需要类似反向布尔绑定的东西。是否有可能或我必须制定一种方法来处理它?
回答by James_D
If you want only a one-way binding, you can use the not()
method defined in BooleanProperty
:
如果您只需要单向绑定,则可以使用中not()
定义的方法BooleanProperty
:
paneWithControls.disableProperty().bind(checkBox.selectedProperty().not());
This is probably what you want, unless you really have other mechanisms for changing the disableProperty()
that do not involve the checkBox
. In that case, you need to use two listeners:
这可能就是您想要的,除非您确实有其他disableProperty()
不涉及checkBox
. 在这种情况下,您需要使用两个侦听器:
checkBox.selectedProperty().addListener((obs, wasSelected, isNowSelected) ->
paneWithControls.setDisable(! isNowSelected));
paneWithControls.disableProperty().addListener((obs, wasDisabled, isNowDisabled) ->
checkBox.setSelected(! isNowDisabled));
回答by Ingo
checkbox.selectedProperty().bindBidirectional(paneWithControls.disableProperty().not());
should work
应该管用
回答by Mustafa
Using the EasyBind libraryone can easily create a new ObservableValue that from checkbox.selectedProperty()
that has the invert of its values.
使用EasyBind 库可以很容易地创建一个新的 ObservableValue,checkbox.selectedProperty()
它的值取反。
paneWithControls.disableProperty().bind(EasyBind.map(checkbox.selectedProperty(), Boolean.FALSE::equals));
回答by borovsky
Something like that? See link below http://docs.oracle.com/javase/8/javafx/api/javafx/beans/binding/BooleanExpression.html#not--
类似的东西?请参阅下面的链接 http://docs.oracle.com/javase/8/javafx/api/javafx/beans/binding/BooleanExpression.html#not--