edvin / tornadofx

Lightweight JavaFX Framework for Kotlin
Apache License 2.0
3.67k stars 269 forks source link

TornadoFx ClassCastException #1343

Open resuwa opened 2 years ago

resuwa commented 2 years ago

As a beginner with Tornadofx and Kotlin I am learning with the nice examples from Edvin Syse. Now I tried the Scopes example(Share view state using a custom scope in a TornadoFX Master/Detail app) and get the the error message "java.lang.ClassCastException: tornadofx.Scope cannot be cast to ...MyScope" I am using Intelij IDEA/Gradle 5.3 /Kotlin 1.4.32/TornadoFx 1.7.14/JRE 1.8 Is it a version issue or do I miss something?

`class CustomerModel: ItemViewModel(){

val custName = bind {item?.custNameProperty }
val country = bind {item?.countryProperty }

} class CustScope :Scope(){ val model= CustomerModel() val customers = FXCollections.observableArrayList(Customer("a","Frankreich"), Customer("b","Italien")) }`

class CustEdit : View("My View") {

override val scope = super.scope as CustScope

val itemsGlobalObject = Locale.getISOCountries().map { Locale("", it) }.observable()
val itemsGlobal = itemsGlobalObject.mapEach { displayCountry }.sorted()

override val root = form {
    prefWidth=300.0
    hboxConstraints { hGrow =Priority.ALWAYS }
    fieldset{
        field("Kunde"){
           // textfield(model.custName)
            textfield(scope.model.custName)
        }
        field("Land"){
            combobox(scope.model.country, itemsGlobal) {
                prefWidth = 125.0
                makeAutocompletable()
            }
        }
    }
SKeeneCode commented 2 years ago

I suspect when you're adding your CustEdit onto the scene graph you are not explicitly putting it into your custom scope.

Something like add(find(CustEdit::class, CustScope())) should work.

However you should not use scopes to hold anything. Create a ViewModal and inject that into your views and controllers:

private val myViewModal: MyViewModal by inject()