After building my scene hierarchy with `override v...
# tornadofx
m
After building my scene hierarchy with
override val root = borderpane { ... }
, how can I set the focus to a specific widget inside this hierarchy? Currently I have a TreeView and a TextArea somewhere nested inside
root
and when the application starts the TreeView always has the focus. However, I'd like to have the TextArea having the focus.
a
Usually it should work if you just call the requestFocus() method on the Node, in this case your TextArea https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Node.html#requestFocus--
m
I've already tried this:
Copy code
override val root = borderpane {
   ...
   treeview<String>(null) {
      ...
   }
                                        
   textarea {
      requestFocus()
      ...
   }
}
But still the focus is captured by the TreeView.
a
hmm that's weird ... probably it could just make sense to call the requestFocus() function after the Scene is visible.
Using this code it gets the focus when the View is docked, which is probably what you want.
Copy code
class MyView : View() {

    private val myTextArea = TextArea()

    override val root = borderpane {
        center = treeview<String> {

        }
        left = myTextArea
    }

    override fun onDock() {
        super.onDock()
        myTextArea.requestFocus()
    }
}
m
👍 That did the trick, thank you!
a
Glad it worked!