# When does focus change?

**URL:** <https://forum.juce.com/t/when-does-focus-change/961>\
**Category:** General JUCE discussion\
**Created:** [July 27, 2006, 9:25pm UTC](https://forum.juce.com/t/when-does-focus-change/961 "2006-07-27T21:25:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![igor](https://avatars.discourse-cdn.com/v4/letter/i/dec6dc/32.png) [@igor](https://forum.juce.com/u/igor)\
**Post date:** [July 27, 2006, 9:25pm UTC](https://forum.juce.com/t/when-does-focus-change/961/1 "2006-07-27T21:25:50Z")

</div>

I’ve added a TextEditor object that allows the user to change the text of a tab label. It floats right above the tab label until the the user hits return (to confirm the change), escapes out, or the label loses focus.

I had thought that focus on the TextEditor would be lost if the user clicked  
anywhere else in the interface. This doesn’t appear be the case. I can start editing one tab label, then click on the next tab over, and the TextEditor keeps floating where it was, with no call to it’s textEditorLostFocus() routine.

Is there a way to tell when the user has clicked outside this component, so I can remove it?

---

<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:** [July 28, 2006, 8:44am UTC](https://forum.juce.com/t/when-does-focus-change/961/2 "2006-07-28T08:44:22Z")

</div>

Focus will only be lost when another comp becomes focused, so clicking somewhere that doesn’t want focus won’t get rid of it. I’m a bit surprised that you’re not getting focus loss events when another text editor appears, so I’ll look into that.

But the best way to do this kind of thing is to make the text editor modal, then override its inputAttemptWhenModal() method to find out when the user tries to click somewhere else. Or better still, just use the Label class instead, which does all of this for you.

---

<div class="post-metadata">

**Author:** ![igor](https://avatars.discourse-cdn.com/v4/letter/i/dec6dc/32.png) [@igor](https://forum.juce.com/u/igor)\
**Post date:** [July 28, 2006, 4:30pm UTC](https://forum.juce.com/t/when-does-focus-change/961/3 "2006-07-28T16:30:34Z")

</div>

Subclassing TextEditor to delete itself in inputAttemptWhenModal() worked out perfectly. Thanks!

In the previous scenario, creating a new TextEditor would cause focus to change, so there’s no bug there to be found.

I started off trying to do this with a floating Label, but I wanted to delete the label when the user hit escape in textEditorEscapeKeyPresed(). Deleting the Label in that function makes the Label unhappy.

---

<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:01am UTC](https://forum.juce.com/t/when-does-focus-change/961/4 "2017-05-12T10:01:11Z")

</div>

Closed on May 12, 2017, 10:01am UTC


