Dynamicaly Creating Many Components?


#1

How can I dynamicaly create many components e.g. I need 100 sliders I don’t want to declare them like below

Slider* slider1;
Slider* slider2;
:
:
Slider* slider100;

Is there a way I can declare a slider array of sort? how?


#2

Sure, if you know the number in advance, just use a pointer array:
Slider* mySliders[100];
And then loop it through and add a slider to each slot.

Or you can use JUCEs OwnedArray which lets you add and drop objects dynamically. And again you can just use a for loop to create as many as you need.


#3

Thanks rock! I don’t know the number in advance, whats a way to create them dynamicaly, if I have to use ownedarray, can you perhaps give code example will be much appreciated.


#4

[code]OwnedArray mySliders;

mySliders.add (new Slider (T(“blah 01”)));
mySliders.add (new Slider (T(“blah 02”)));

Slider* slider = mySliders.getUnchecked (1);

mySliders.clear (true);
[/code]

ah if you are going to use a LLOT of OwnedArrays, you should consider using the VoidArray, is a bit more verbose to write (due to the frequent typecast you have to do or the manual object free), but should maintain the code size low…


#5

…and if you have like a hundred of them that can be placed and managed logically, you do something like this:

	for(int i = 0; i < numSliders; i++)
	{
		sliderArray.add(new Slider(String(i)));
		sliderArray.getLast()->setTopLeftPosition(i*32,0);
		addAndMakeVisible(sliderArray.getLast());
	}

#6