Label - single click callback with edit on double click- how to?

Hi Guys,

What’s the best/recommended way to hande single click on Label, when Label is set to edit on doubleclick with Label.setEditable(false, true, false) ?
I would like to perform some actions upon single click, and edit on double click.
Thanks in advance for any hints!

If you plan to have this behaviour for several labels, then you could inherit Label :

struct SpecialLabel : public Label
{
    using Label::Label;

    void mouseUp (const MouseEvent& e) override
    {
        Label::mouseUp (e);

        // changing the text colour everytime the label is clicked :
        Colour randomColour (Random::getSystemRandom().nextInt());
        setColour (Label::textColourId, randomColour);
    }
};

//==============================================================================
class MainComponent   : public Component
{
public:
    //==============================================================================
    MainComponent()
    {
        label.setEditable (false, true, false);
        addAndMakeVisible (label);
        setSize (400, 200);
    }

    void paint (Graphics& g) override { g.fillAll (Colours::lightgrey); }

    void resized() override { label.setBounds (10, 10, 200, 30); }

private:
    //==============================================================================
    SpecialLabel label {"labelName", "this is the label text"};

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};

But if it’s something you just want to do once, then you can simply listen to the mouse events coming from the label :

//==============================================================================
class MainComponent   : public Component
{
public:
    //==============================================================================
    MainComponent()
    {
        label.setEditable (false, true, false);
        label.addMouseListener (this, false);
        addAndMakeVisible (label);
        setSize (400, 200);
    }

    ~MainComponent()
    {
        label.removeMouseListener (this);
    }

    void paint (Graphics& g) override { g.fillAll (Colours::lightgrey); }

    void resized() override { label.setBounds (10, 10, 200, 30); }

    void mouseUp (const MouseEvent& e) override
    {
        if (e.eventComponent == &label)
        {
            Colour randomColour (Random::getSystemRandom().nextInt());
            label.setColour (Label::textColourId, randomColour);
        }
    }

private:
    //==============================================================================
    Label label {"labelName", "this is the label text"};

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};
1 Like

Excellent info!
Thank you!