Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to bind inverse boolean, JavaFX

My goal is to bind these two properties such as when checkbox is selected then paneWithControls is enabled and vice-versa.

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?

like image 347
Tomasz Mularczyk Avatar asked Apr 13 '15 22:04

Tomasz Mularczyk


3 Answers

If you want only a one-way binding, you can use the not() method defined in 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:

checkBox.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> 
    paneWithControls.setDisable(! isNowSelected));

paneWithControls.disableProperty().addListener((obs, wasDisabled, isNowDisabled) ->
    checkBox.setSelected(! isNowDisabled));
like image 90
James_D Avatar answered Nov 16 '22 12:11

James_D


checkbox.selectedProperty().bindBidirectional(paneWithControls.disableProperty().not());

should work

like image 43
Ingo Avatar answered Nov 16 '22 13:11

Ingo


Using the EasyBind library one can easily create a new ObservableValue that from checkbox.selectedProperty() that has the invert of its values.

paneWithControls.disableProperty().bind(EasyBind.map(checkbox.selectedProperty(), Boolean.FALSE::equals));
like image 1
Mustafa Avatar answered Nov 16 '22 12:11

Mustafa