Adding view with constructor arguments to a borderpane

蓝咒 提交于 2019-12-04 22:52:37

You should absolutely never manually instantiate a View or Fragment. You can however use find with a special argument that includes the parameters that should be passed to the View or Fragment. Keep in mind that a View will only be instantiated once in the current scope, so if you need to open several UIComponents of the same type, initialized with different parameters, make sure they are Fragments.

See the Components section of the guide for more information:

https://github.com/edvin/tornadofx-guide/blob/master/part1/3.%20Components.md

Search for "Passing Parameters to Views" in the document above.

That said, it's almost always better to use scopes to pass information to views. You can read more about these best practices in the guide.

Note that you cannot add constructor parameters to your ui components, since the framework needs a no args constructor to be able to instantiate the class. Parameters are passed using the by param extension, like this:

class AudioView : Fragment("AudioView") {
    val playFromFile: Boolean by param()

    override val root = vbox {
    }
}

Also note that to be able to get multiple instances in the same scope you need to use Fragment, not View, since Views are singletons within a scope.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!