You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by "Tx. T" <tx...@yahoo.com> on 2017/01/20 13:23:48 UTC

GroovyFx binding to two properties

how would I go about binding the textProperty of two textfields to a button's disable (on/off)?  Here is an example of binding to 1 textfield.  I like to know how to do similar but with 2 or more textfields.Basically, if two textfields are empty, the submit button is disable, else.#!/usr/bin/env groovy

import static groovyx.javafx.GroovyFX.start

start {
    stage title: "Test App", visible: true, {
        scene {
            hbox {
                tf = textField(id: "tf")
                button('Submit', id: "smb", disable: bind(tf.textProperty()).using { it.isEmpty() })
            }
        }
    }
}

Re: GroovyFx binding to two properties

Posted by "Tx. T" <tx...@yahoo.com>.
I did this but thought this was not so Groovy-ish :)

#!/usr/bin/env groovy
import static groovyx.javafx.GroovyFX.startimport javafx.beans.binding.Bindings as B
start {    stage title: "Test App", visible: true, {        scene {            hbox {                tf1  = textField(id: "tf1")                tf2  = textField(id: "tf2")
                button('Submit', id: "smb", disable: bind(B.or(tf1.textProperty().isEmpty(),                                                               tf2.textProperty().isEmpty())))            }        }    }}


      From: Andres Almiray <aa...@gmail.com>
 To: users@groovy.apache.org; Tx. T <tx...@yahoo.com> 
 Sent: Friday, January 20, 2017 7:36 AM
 Subject: Re: GroovyFx binding to two properties
   
You must create a composite binding. The standard JavaFX API provides a `javafx.beans.binding.Bindings` class that can be used to create a BooleanBinding out of a group of ObservableValues.
There's also EasyBind (my personal favorite) https://github.com/TomasMikula/EasyBind

You may create a BooleanBinding for each textfield that computes if said textfield is empty or not. Then you may create a composite BooleanBinding based on the previous boolean bindings.
Or you can create a single BooleanBinding based on the textProperty of each textfield directly. Your choice.

Once you've got the binding simply apply it to the button's disable property.

Cheers,
Andres

On Fri, Jan 20, 2017 at 2:23 PM, Tx. T <tx...@yahoo.com> wrote:

how would I go about binding the textProperty of two textfields to a button's disable (on/off)?  Here is an example of binding to 1 textfield.  I like to know how to do similar but with 2 or more textfields.Basically, if two textfields are empty, the submit button is disable, else.#!/usr/bin/env groovy

import static groovyx.javafx.GroovyFX.start

start {
    stage title: "Test App", visible: true, {
        scene {
            hbox {
                tf = textField(id: "tf")
                button('Submit', id: "smb", disable: bind(tf.textProperty()).using { it.isEmpty() })
            }
        }
    }
}



   

Re: GroovyFx binding to two properties

Posted by Andres Almiray <aa...@gmail.com>.
You must create a composite binding. The standard JavaFX API provides a
`javafx.beans.binding.Bindings` class that can be used to create a
BooleanBinding out of a group of ObservableValues.
There's also EasyBind (my personal favorite)
https://github.com/TomasMikula/EasyBind

You may create a BooleanBinding for each textfield that computes if said
textfield is empty or not. Then you may create a composite BooleanBinding
based on the previous boolean bindings.
Or you can create a single BooleanBinding based on the textProperty of each
textfield directly. Your choice.

Once you've got the binding simply apply it to the button's disable
property.

Cheers,
Andres

On Fri, Jan 20, 2017 at 2:23 PM, Tx. T <tx...@yahoo.com> wrote:

> how would I go about binding the textProperty of two textfields to a button's disable (on/off)?  Here is an example of binding to 1 textfield.  I like to know how to do similar but with 2 or more textfields.
>
> Basically, if two textfields are empty, the submit button is disable, else.
>
> #!/usr/bin/env groovy
>
> import static groovyx.javafx.GroovyFX.start
>
> start {
>     stage title: "Test App", visible: true, {
>         scene {
>             hbox {
>                 tf = textField(id: "tf")
>                 button('Submit', id: "smb", disable: bind(tf.textProperty()).using { it.isEmpty() })
>             }
>         }
>     }
> }
>
>