# TextEditor highlight not working properly

**URL:** <https://forum.juce.com/t/texteditor-highlight-not-working-properly/42109>\
**Category:** General JUCE discussion\
**Created:** [October 8, 2020, 8:47am UTC](https://forum.juce.com/t/texteditor-highlight-not-working-properly/42109 "2020-10-08T08:47:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jellebakker](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jellebakker/32/17053_2.png) [@jellebakker](https://forum.juce.com/u/jellebakker)\
**Post date:** [October 8, 2020, 8:47am UTC](https://forum.juce.com/t/texteditor-highlight-not-working-properly/42109/1 "2020-10-08T08:47:27Z")

</div>

Hi everyone,

I’m having trouble getting the highlight of TextEditors to work properly.  
I would like the text editors to highlight all the text when a user clicks on it.

To do that, I tried initialising the editors with `setSelectAllWhenFocused(true)`.  
But, when the editor gains focus, it doesn’t highlight any text, so that doesn’t work.  
I did some debugging and under the hood it seems that the texts is highlighted for a short time in TextEditor::focusGained, but quickly after that a `mouseDown` callback removes the highlighted range.

I looked into the projucer code and found that instead of using TextEditor, the projucer uses TextPropertyComponent to edit single line settings. Isn’t the TextEditor designed editing single line properties?

Another issue I have is that the texteditor stays highlighted even when it loses focus.  
Here is a code example that shows these problems, I hope someone can help me out.

```
class MainComponent : public juce::Component
{
public:  
    MainComponent()
    {
        addAndMakeVisible(editor1_);
        addAndMakeVisible(editor2_);
        
        editor1_.setMultiLine(true);
        editor2_.setMultiLine(true);
        
        editor1_.setTextToShowWhenEmpty("Click should highlight", juce::Colours::grey);
        editor2_.setTextToShowWhenEmpty("Click should highlight", juce::Colours::grey);
    
        editor1_.setSelectAllWhenFocused(true);
        editor2_.setSelectAllWhenFocused(true);
        
        addAndMakeVisible(button_);
        
        setSize (600, 400);
    }
    
    ~MainComponent() override {}
    
    void paint (juce::Graphics& g) override
    {
        g.fillAll (getLookAndFeel().findColour (juce::ResizableWindow::backgroundColourId));
    }
    
    void resized() override
    {
        editor1_.setBounds(10, 10, 200, 30);
        editor2_.setBounds(10, 50, 200, 30);
        
        button_.setBounds(10, 90, 150, 30);
    }
    
private:
    juce::TextEditor editor1_;
    juce::TextEditor editor2_;
    
    juce::TextButton button_ {"Lose focus"};
    
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};
```

---

<div class="post-metadata">

**Author:** ![ed95](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ed95/32/7635_2.png) [@ed95](https://forum.juce.com/u/ed95)\
**Post date:** [October 9, 2020, 2:09pm UTC](https://forum.juce.com/t/texteditor-highlight-not-working-properly/42109/2 "2020-10-09T14:09:37Z")

</div>

Thanks for reporting. We’ve pushed a fix for the `setSelectAllWhenFocused()` bug in [56252ca](https://github.com/juce-framework/JUCE/commit/56252ca098b923ec2fcc68d725b81efac21c3439).

> I looked into the projucer code and found that instead of using TextEditor, the projucer uses TextPropertyComponent to edit single line settings. Isn’t the TextEditor designed editing single line properties?

`TextPropertyComponent` uses a `TextEditor` internally for text input, the class is just a wrapper around managing the text value and allows it to be added to `PropertyPanel`s in the UI.

> the texteditor stays highlighted even when it loses focus.

This is intentional behaviour. You could modify this by clearing the selection on focus loss if you wanted.
