Skip to content
This repository has been archived by the owner on Sep 28, 2024. It is now read-only.

TornadoFx ClassCastException #1343

Open
resuwa opened this issue Oct 15, 2021 · 1 comment
Open

TornadoFx ClassCastException #1343

resuwa opened this issue Oct 15, 2021 · 1 comment

Comments

@resuwa
Copy link

resuwa commented Oct 15, 2021

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
Copy link

SKeeneCode commented Oct 25, 2021

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()

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants