How do I get the focus to my TextEditor?

I’m bringing up a DocumentWindow with a read-only TextEditor and an editable TextEditor, but the focus is not going to the editable TextEditor when the window comes up. I tried grabKeyboardFocus() in the content component constructor but that didn’t help. What should I do?

This question keeps getting asked, because people seem to expect that they can focus on a component in a constructor - but obviously when it’s not yet actually visible on the screen in a window, it can’t possibly become focused.

I’m a bit tired of repeatedly explaining this, so might try adding an assertion when you attempt to focus on a non-visible component. Not sure it that might trigger too many false positives, though…

To be honest, I was hoping Juce would automatically realize that since my Component hierarchy has only one Component that could actually use the keyboard focus (the editable TextEditor), it would automatically set the focus to that Component.

I mean, isn’t that what the KeyboardFocusTraverser is for? I thought the default one was smart about the various Component subclasses.

And FYI I am calling grabKeyboardFocus() after Component::setVisible(true) on the DocumentWindow.

1 Like

Okay after some fiddling, I got it to work by calling setWantsKeyboardFocus(false) on everything except the editable TextEditor (including the DocumentWindow and the content Component).