https://kotlinlang.org logo
m

matthjes

01/24/2020, 2:20 PM
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

Arthur

01/24/2020, 2:32 PM
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

matthjes

01/24/2020, 2:49 PM
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

Arthur

01/24/2020, 7:46 PM
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

matthjes

01/25/2020, 7:22 AM
👍 That did the trick, thank you!
a

Arthur

01/25/2020, 8:04 AM
Glad it worked!