Label & setInputRestrictions


Is it possible to add setInputRestrictions to Label ?

In the Label class add two private members:

    int maxTextLength;  
    String allowedCharacters;

and add the public method:

    /** Sets limits on the characters that can be entered.
     @param maxTextLength        if this is > 0, it sets a maximum length limit; if 0, no
     limit is set
     @param allowedCharacters    if this is non-empty, then only characters that occur in
     this string are allowed to be entered into the editor.
    void setInputRestrictions (int maxTextLength,
                               const String& allowedCharacters = String::empty);

In the Label constructor add:

Label::Label (const String& name,
              const String& labelText)
    : Component (name),
      textValue (labelText),
      lastTextValue (labelText),
      font (15.0f),
      justification (Justification::centredLeft),
      horizontalBorderSize (5),
      verticalBorderSize (1),
      minimumHorizontalScale (0.7f),
      editSingleClick (false),
      editDoubleClick (false),
      lossOfFocusDiscardsChanges (false),
      maxTextLength (-1)
    setColour (TextEditor::textColourId, Colours::black);
    setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
    setColour (TextEditor::outlineColourId, Colours::transparentBlack);

    textValue.addListener (this);

to initialize maxTextLength to -1.

Add the method:

void Label::setInputRestrictions (const int maxLen,
                                       const String& chars)
    maxTextLength = jmax (0, maxLen);
    allowedCharacters = chars;
    if (editor)
        editor->setInputRestrictions(maxTextLength, allowedCharacters);

and finally modify Label::showEditor() :

void Label::showEditor()
    if (editor == nullptr)
        addAndMakeVisible (editor = createEditorComponent());
        editor->setText (getText(), false);
        editor->addListener (this);
        editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
        if (maxTextLength != -1)
            editor->setInputRestrictions(maxTextLength, allowedCharacters);


        editorShown (editor);

        enterModalState (false);




Yeah, but it really needs to be done with some kind of input restriction virtual class that can filter the input in any way, rather than just providing hard-coded limits on the character set and length. I’ve been meaning to do that for a long time, so I might throw something together.


Jules, did you modify the Label class in terms of input restrictions ?



I can't remember - this post is from 2 years ago! Have a look at the class and see if it does what you want!


With the function editorShown from the Label::Listener Class you can get the shown TextEditor, for which you than can set the input restriction. Does this answer you question?


I tried overriding editorShown() and it never got into my subclassed method… so here’s a bump to get something official added to JUCE.




For anyone looking for answer - it is possible to set the restrictions on Label by overriding createEditorComponent() method of Label.

virtual TextEditor* createEditorComponent() override;


TextEditor* YourCustomLabel::createEditorComponent()
    TextEditor* const ed = Label::createEditorComponent();
    ed->setInputRestrictions(yourMaxTextLength, yourCharactersAllowed);
    return ed;

@jules It would be cool if you’d just add the setInputFilter(newFilter) method on Label so we could pass LengthAndCharacterRestriction or whatever else into the Label instance. And then one line in the Label::createEditorComponent() method:

ed->setInputFilter(inputFilter, false);