Setting focus manually


#1

i’m traversing the juce tree, for finding a function in the gui components. i need to manually set focus to my own component, before running a modal loop on it. if i can’t give focus, subsequent keypresses aren’t dispatched to that component until the user manually click on it. i see there is the Component::setFocusOrder, or i have to call setWantsKeyboardFocus(true) for that component (but what if other components steal the focus).
how can i turnaround a lack of a Component::setFocus() ?


#2

doh… i answered myself… i didn’t noticed toFront() with shouldAlsoGainFocus boolean… maybe is the correct answer ?


#3

There’s also grabKeyboardFocus(), if you don’t want to change the z-order of the comp.


#4

oh yes, i see that and fixed all my problems… thanx


#5

Hi,

I want to set the focus to a given control in a dialog box, on startup.

I tried using m_ptMacroList->setWantsKeyBoardFocus(true).
Also tried using m_ptMacroList->toFront(true), didnt work either.
I also tried using m_ptMacroList->grabKeyboardFocus() to set the focus to this control, during dialog startup.

I am using these calls in the constructor of the dialog after addAndMakeVisible.

m_ptMacroList is a pointer to Juce TableListBox class.

Thanks,

Kiran


#6

Eek! It’s not a good idea to add your component to another one while you’re in the middle of constructing it!

And it’s obviously impossible for a component to have the focus unless it’s actually on-screen and visible - you’ll have to call grabFocus() after it has been fully created and added to some kind of real, visible window.


#7

Thanks for a quick reply.

Where do you suggest me to use the grabFocus() call, incase if I need my control to be highlighted once the dialog pops up.


#8

You’d need to call it after the dialog is actually visible.

A better way to do it might just be to make sure that your comp will be the first to get the focus by default - have a look at Component::setExplicitFocusOrder and Component::setFocusContainer


#9

Thanks for the help.


#10

Well I’m also a bit confused on this and can’t find any examples.

I have a component being created in a showModalDialg() call. In the constructor of the component I add 2 buttons and yet two complex components both consisting of a combobox and two text editors.

In the end I want focus to be at one of the combobox components while the dialog is shown but so far I haven’t been able to give it to even one of the buttons.
You say grabKeyboardFocus shouldn’t be called in the constructors but where then? I’ve tried resized() without luck. I have even tried to add visibilityChanged() calling it if isVisible() is true. What other possibilities do I have?

While first having clicked at any of the components it works fine TABing around between all 8 subcomponents

I might have to add that I’m working at Ubuntu. Consider trying it at Windows too see if it is different there


#11