Dynamicaly Creating Many Components?


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?


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.


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.


[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);

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…


…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)));