# Setting focus manually

**URL:** <https://forum.juce.com/t/setting-focus-manually/824>\
**Category:** General JUCE discussion\
**Created:** [May 2, 2006, 10:37am UTC](https://forum.juce.com/t/setting-focus-manually/824 "2006-05-02T10:37:22Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![kunitoki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kunitoki/32/15_2.png) [@kunitoki](https://forum.juce.com/u/kunitoki)\
**Post date:** [May 2, 2006, 10:37am UTC](https://forum.juce.com/t/setting-focus-manually/824/1 "2006-05-02T10:37:22Z")

</div>

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() ?

---

<div class="post-metadata">

**Author:** ![kunitoki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kunitoki/32/15_2.png) [@kunitoki](https://forum.juce.com/u/kunitoki)\
**Post date:** [May 2, 2006, 10:48am UTC](https://forum.juce.com/t/setting-focus-manually/824/2 "2006-05-02T10:48:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 2, 2006, 11:26am UTC](https://forum.juce.com/t/setting-focus-manually/824/3 "2006-05-02T11:26:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kunitoki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kunitoki/32/15_2.png) [@kunitoki](https://forum.juce.com/u/kunitoki)\
**Post date:** [May 2, 2006, 1:24pm UTC](https://forum.juce.com/t/setting-focus-manually/824/4 "2006-05-02T13:24:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kirankumarr](https://avatars.discourse-cdn.com/v4/letter/k/b487fb/32.png) [@kirankumarr](https://forum.juce.com/u/kirankumarr)\
**Post date:** [September 22, 2010, 5:34pm UTC](https://forum.juce.com/t/setting-focus-manually/824/5 "2010-09-22T17:34:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [September 22, 2010, 5:57pm UTC](https://forum.juce.com/t/setting-focus-manually/824/6 "2010-09-22T17:57:00Z")

</div>

> [@](#):
>
> I am using these calls in the constructor of the dialog after addAndMakeVisible.

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.

---

<div class="post-metadata">

**Author:** ![kirankumarr](https://avatars.discourse-cdn.com/v4/letter/k/b487fb/32.png) [@kirankumarr](https://forum.juce.com/u/kirankumarr)\
**Post date:** [September 22, 2010, 6:08pm UTC](https://forum.juce.com/t/setting-focus-manually/824/7 "2010-09-22T18:08:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [September 22, 2010, 7:44pm UTC](https://forum.juce.com/t/setting-focus-manually/824/8 "2010-09-22T19:44:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kirankumarr](https://avatars.discourse-cdn.com/v4/letter/k/b487fb/32.png) [@kirankumarr](https://forum.juce.com/u/kirankumarr)\
**Post date:** [September 28, 2010, 4:57pm UTC](https://forum.juce.com/t/setting-focus-manually/824/9 "2010-09-28T16:57:24Z")

</div>

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![KimBP](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@KimBP](https://forum.juce.com/u/KimBP)\
**Post date:** [November 1, 2010, 7:34pm UTC](https://forum.juce.com/t/setting-focus-manually/824/10 "2010-11-01T19:34:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 12, 2017, 10:02am UTC](https://forum.juce.com/t/setting-focus-manually/824/11 "2017-05-12T10:02:23Z")

</div>


