Um... hi :slightly_smiling_face: Really new to Jav...
# tornadofx
w
Um... hi ­čÖé Really new to Java/Kotlin, and asking for help implementing my first serious application. It's a connector that interfaces GPS reports (and some other things) from digital radios into a really legacy and clunky mapping software (OziExplorer, anyone? dammit... its API is horrendous!) Now for the TornadoFX question: how should I implement a UI item that would change its caption depending on an external Boolean fun? Seems like I'd still need to bind this to a property and somehow tell TFX to refresh it?
s
Quick clarification, what do you mean by caption? Are you referring to a tooltip or something else?
w
I mean, can I change the text on a button by some external event?
also I could really use a review of this little project, well, I'm only learning so tips are very welcome
s
Yes you can bind the TextProperty of the button:
Copy code
val myStringProperty = SimpleStringProperty()
button {
    textProperty().bind(myStringProperty)
}
Ah I see you wanted it to change its text based on a boolean? One sec
Copy code
val myBoolProp = SimpleBooleanProperty()

button {
    textProperty().bind(myBoolProp.stringBinding {
        if (it == true) {
            "button text 1"
        } else {
            "button text 2"
        }
    })
}
w
How do observables really work? What if I need to bind that property to some external function call? Like,
if (radioController.isConnected()) isConnectedProperty.set(true)
what is the proper way of doing this?
s
So you have a boolean property called isConnectedProperty? Just need a reference to it from somewhere and then you could do:
Copy code
val buttonBinding = isConnectedProperty.stringBinding {
    if (it == true) {
        "Disconnect"
    } else {
        "Connect"
    }
}
button {
    textProperty().bind(buttonBinding)
}
I don't know too much about the inner working of javafx observables. When a value of an observable changes it fires a change event which you can listen in on. I would assume bind() is shorthand for attaching a listener to the property.
You could also do
Copy code
button {
    isConnectedProperty.onChange { 
        text = if (it) "disconnect" else "connect"
    }
}