Put a StringArray in a ListBox

Hello.
Please tell me how to put a StringArray in a ListBox? I looked at examples, but there is some kind of hell with XML and other incomprehensible things. I’ve already created the ListBox.
I thought there was something like:

listbox.setText(index, "text");

but found nothing.

There is no simple method to put anything in a ListBox. What is shown in a ListBox instance is determined by a ListBoxModel instance and there is no ready to use implementation in Juce included to handle a StringArray.

So, you need to make a ListBoxModel subclass that deals with your StringArray.

1 Like

I suggest having a look at the examples provided in the repository. The FontsDemo makes use of the list and shows how to set up a ListBoxModel.

This is done in two parts: the inheritance and then the overriding/filling of the required methods:

1 Like

OK, thanks! But the JUCE developers could have made it easier.

I’m not really sure what you mean. The list box is a concept: a proper implementation abstracts away the “what” from the “how” - in your case, text (the “what”) to be displayed.

The list box doesn’t require displaying text as it can be used to draw any number of things; in JUCE’s case this can include Components themselves, so you can customise the list to whatever you want.

If you wanted to draw an image beside the text, what would you do? Well, you wouldn’t want to be stuck with a text-only option.

Maybe the easiest option is for the JUCE devs to provide a tutorial on the website.

Something happened:

#pragma once

using namespace juce;

//==============================================================================
class myApplication :
    public Component,
    private ListBoxModel
{
public:
    //==============================================================================
    myApplication()
    {
        addAndMakeVisible (listBox);
        listBox.setRowHeight (20);
        listBox.setModel (this); // Tell the listbox where to get its data model
        listBox.setColour (ListBox::textColourId, Colours::black);
        listBox.setColour (ListBox::backgroundColourId, Colours::lightgrey);

        setSize (300, 200);

        myVoid();
    }

    void myVoid ()
    {
        stringList.set(0, "text 1");
        stringList.set(1, "text 2");
        listBox.updateContent();
    }

    //==============================================================================
    void paint (Graphics& g) override
    {
        g.fillAll (Colours::grey);
    }

    void resized() override
    {
        listBox.setBounds (10, 10, getWidth() - 20, getHeight() - 20);
    }

    // The following methods implement the ListBoxModel virtual methods:
    int getNumRows() override
    {
        return stringList.size();
    }

    void paintListBoxItem (int rowNumber, Graphics& g,
                           int width, int height, bool rowIsSelected) override
    {
        if (rowIsSelected)
            g.fillAll (Colours::lightblue);

        AttributedString s;
        s.setWordWrap (AttributedString::none);
        s.setJustification (Justification::centredLeft);
        s.append (stringList[rowNumber], Font (16, Font::italic), Colours::black);

        s.draw (g, Rectangle<int> (width, height).expanded (-4, 50).toFloat());
    }

private:

    ListBox listBox;
    StringArray stringList;

    //==============================================================================
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (myApplication)
};

“Something happened”? What does that mean? It worked? It didn’t work? You got a compile error? Please let us know what it is you are trying to say here. If you need more help, let us know what the problem is that you need help with, so we don’t have to try to guess, ok?

1 Like

Yes, it works.