"...It seems the window doesn't have focus at all when opened, because when I click on the window's background, the editor does get selected."
I also encountered this problem (on Windows) the other day, but I don't think it's a bug of JUCE, maybe the OS or something else I'm not sure. I did this to solve it (notice I didn't use grabKeyboardFocus() for the child component, instead of its parent's parent, and the sentence places in its parent's parent's parent's constructor :) ):
In the top level component: DocumentWindow's constructor:
setWantsKeyboardFocus (false);
mainContentComponent->grabKeyboardFocus();
mainContentComponent has a child component (login ui), the child has a textEditor, in the child's constructor, I added:
// class PasswordInut inherits from TextEditor
addAndMakeVisible (passwordTextEdit = new PasswordInput());
passwordTextEdit->setExplicitFocusOrder (0);
allOtherChilds->setWantsKeyboardFocus (false);
// the focus order should start at 1
// edited: 2015-07-22 19:37 Thank for stijnfrishert
passwordTextEdit->setExplicitFocusOrder (1);
When the whole app begin to run, the first interface is the child component (of course, the DocumentWindow also showed up), the keyboard focus locates exactly on the passwordTextEdit. All the platforms are the same (OSX, Linux, Windows, iOS, Android).
In other words, if you want a component has focus and insist doing this in the 'nearest upper class's' constructor, I think it's better in its parent's parent's constrcutor, even in the top level comp or the JUCEApplication::initialise(). Make sure all components have showed up, then doing this. Perhaps I'm wrong...again...
Back to your question, appoint the keyboard focus on a very child compoennt when its parent showing up, and even more complex (for example: after x seconds or minutes, if the user hasn't input anything, make the focus auto transfer to another component, and so on...) Such requirements I needed at least for 3 situations in my recent project. Each of them, I adopted different strategies. The most important thing is: make sure the comp and all its upper parents have showed up, then appoint the focus.