Search code examples
kotlintornadofx

TornadoFX get reference to a tab and get its Stage


How can I get a reference to the first tab? And furthermore how do I get its Stage?

class MainApp : App() {
    override val primaryView = MainView::class

    class MainView : View() {
        override val root = VBox()

        init {
            with(root) {
                tabpane {
                    tab("Report") {
                        hbox {
                            // TODO Want a reference to this tab here.
                            // Ideally something like tab.getStage()
                            this += Button("Hello 1")
                        }
                    }
                    tab("Data Entry") {
                        hbox {
                            this += Button("Hello 2")
                        }
                    }
                }
            }
        }
    }
}

Solution

  • Quickly: I've seen a lot of your posts here and they're pretty basic questions. These are things you could figure out on your own if you did your own digging. I'd recommend at least looking at the official guide to get a good grasp on most of what you need to know. Then, check out other posts on here to see if they've been answered already.

    But to answer your question:

    class MainView : View() {
        override val root = vbox {
            tabpane {
                tab("Report") {
                    hbox {
                        val tab = this@tab //Here is your tab
                        button("Hello 1")
                    }
                }
                tab("Data Entry") {
                    hbox {
                        button("Hello 2")
                    }
                }
            }
        }
    }
    

    Again, I would urge you to look at the guide, as you missed some helpful building tools (see how I built the buttons? see how I moved the root out of init?). I'd hate for you to code more than you need to then realize you could've done less work if you had known how.

    Also: Tabs don't have references to stages. They just inherit Styleable and EventTarget, they're not like Views or Fragments.