[solved] Syntax for adding array of items to a JUCE Grid?

Hi there.

I am using JUCE Grids to arrange many GUI items. Currently I am doing so by manually typing in each item, such as with the following:

frequencyEditorGrid.items = { juce::GridItem (voiceFrequencyBoxes[0]), juce::GridItem (voiceFrequencyBoxes[1]), juce::GridItem (voiceFrequencyBoxes[2]), juce::GridItem (voiceFrequencyBoxes[3]), juce::GridItem (voiceFrequencyBoxes[4]), juce::GridItem (voiceFrequencyBoxes[5]), juce::GridItem (voiceFrequencyBoxes[6]), juce::GridItem (voiceFrequencyBoxes[7]), juce::GridItem (voiceFrequencyBoxes[8]), juce::GridItem (voiceFrequencyBoxes[9]), juce::GridItem (voiceFrequencyBoxes[10]), juce::GridItem (voiceFrequencyBoxes[11]), juce::GridItem (voiceFrequencyBoxes[12]), juce::GridItem (voiceFrequencyBoxes[13]), juce::GridItem (voiceFrequencyBoxes[14]), juce::GridItem (voiceFrequencyBoxes[15]) };

However, I don’t like this for a few reasons- it’s tedious, it clutters code, and it requires the magic number of voiceFrequencyBoxes pasted in, which is annoying if I change the amount later. I’d prefer to just iterate over my voiceFrequencyBoxes array, adding them as items to the Grid- but I can’t seem to figure out the syntax for doing so.

Is it possible? What would the syntax be? Thank you!

A trusty old for-loop should do the job:

for (auto& box : voiceFrequencyBoxes)
    frequencyEditorGrid.items.add(juce::GridItem{ box });
1 Like

thanks Jimmi- I’ve been using c style arrays rather than JUCE Arrays and so I didn’t know about the add method. Should probably make the switch! I always seem to miss obvious things like this when looking through the docs :sweat_smile:

1 Like