Search code examples
kotlinjavafxtornadofx

Reuse elements in TornadoFX


How to make components reusable in TornadoFX? Lets assume I have tableview defined like this:

tableview<MyObj>(items) {
    column("name", MyObj::name)
    column("value", MyObj::value)
}

Can I somehow wrap it to reuse the same columns in another views? To use it like

mytableview(items)

This way I won't duplicate any columns.


Solution

  • Make a factory function!

    fun EventTarget.myobjtable(items: ObservableList<MyObj>, op: TableView<MyObj>.() -> Unit = {}) =
        tableview(items) {
            column("name", MyObj::name)
            column("value", MyObj::value)
            op()
        }
    

    This should work the same as calling the original tableview function, just with the columns already added in.