Editable label's TextEdit centered text

I can make a Label's text centered, however there is no way to make its edit box's text centered, is there?

1 Like

No, sorry - never got around to adding justification to TextEditors.. It's been requested before, I should do it at some point..

Thanks, that's what I was thinking. A first implementation would be that the editor automagically follows the label's alignment (sounds logic).

i've too have come upon a situation where this would be really nice..

although i'd like right justification 

me too...

I’d really like to see support for Justification added to TextEditor, in particular so that a Label’s TextEditor can match the alignment of the Label itself.

There are lots of threads requesting this… pretty please… :kissing_closed_eyes:


I’m newer to JUCE, but I really would appreciate this feature. Any word on this?

1 Like

While this would be a really useful feature, you can work around it reasonably well in simple cases and where there is only a single line.

Create your own custom Label subclass and override Label::createEditorComponent() where you can create your own subclass of TextEditor. This text editor needs to be a ComponentListener and TextEditor::Listener and listen to itself in both cases.

In the listener callbacks TextEditor::textEditorTextChanged and ComponentListener::componentMovedOrResized you can measure the text using TextEditor::getTextWidth() and TextEditor::getTextHeight(). Then you can resize the TextEditor so that it is just big enough to hold the line of text and position it wherever you like within the parent.


Thanks @martinrobinson, got it working with your advice.

For my own and other’s future reference (and easy copy and paste), here are the Label and TextEditor subclasses:
I added the KeyListener callback so that it keeps the editor entered while the user types.


This seems to be one small change to JUCE, but a much anticipated one. Can we pretty-please have this in a future JUCE version? :relaxed:

+1 :slight_smile:

Agreed, this should just be the default.

For quick and dirty setting of the text editor’s justification, I found a simpler solution than those listed above: just subclass Label and override editorShown like this:

/** Called when the text editor has just appeared, due to a user click or other focus change. */
void editorShown (TextEditor* editor) override
    editor->setJustification (Justification::centred);

(Note this is the Label’s own editorShown method, not to be confused with the editorShown method of Label::Listener.)

Still feels like a hack, though, as I’m subclassing just to apply a visual style change.