Search code examples
comboboxautocompletetornadofx

How to update autocomplete combobox suggestion list with info from the database (TornadoFX)?


I have an auto complete combobox that works well but suggestion list does not update after runAsync operation is completed.

import javafx.beans.property.SimpleObjectProperty
import javafx.collections.ObservableList
import tornadofx.*

class TestBox : View("My View") {

var treatment = SimpleObjectProperty<String>()
var treatmentList = SimpleObjectProperty<ObservableList<String>>()
override val root = borderpane {
    center {
        form {
            fieldset {
                field("Fruit") {
                    combobox(treatment, treatmentList.value) {
                        makeAutocompletable()
                    }
                }
            }
        }
    }
}
init {
    runAsync {
        listOf("Rice", "beans", "Yams").observable()
    } ui {
        treatmentList.set(it)
    }
}
}

What am I missing out? Thanks.


Solution

  • You are not using the ObservableList. You are changing the contents of the property containing the list. The combobox never knows about the treatmentList property, therefore it cannot observe on it. Try making treatmentList an ObservableList<String>. Then, you can do treatmentList.setAll(it) in runAsyncs ui block.

    import javafx.beans.property.SimpleObjectProperty
    import tornadofx.*
    
    class TestBox : View("My View") {
        var treatment = SimpleObjectProperty<String>()
        var treatmentList = mutableListOf<String>().observable()
        override val root = borderpane {
            center {
                form {
                    fieldset {
                        field("Fruit") {
                            combobox(treatment, treatmentList) {
                                makeAutocompletable()
                            }
                        }
                    }
                }
            }
        }
        init {
            runAsync {
                listOf("Rice", "beans", "Yams")
            } ui {
                treatmentList.setAll(it)
            }
        }
    }