Link to home
Start Free TrialLog in
Avatar of snajalm
snajalm

asked on

Dynamically generated object names!

Hi,

I would like to create a series of auto-generating variable names that can be used to instantaite object depending on an ArrayList size!  For example if I want to creat a series jTextArea based on the number of a certain ArrayList, the I would be creating ArrayList.size() number of jTextAreas like

jTextArea1
jTextArea2
jTextArea3
jTextArea4

so that

JTextArea jTextArea( i ) = new JTextArea();

How would I be able to do that??!
Avatar of for_yan
for_yan
Flag of United States of America image

You would be better off if you have an array of JTextArea

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Somthing like this:

JTextArea[] areas = new JTextArea[arraylist.size()];
for(int j=0; j<areas.length; j++){
area[j] = new JTextArea(20);
}

Open in new window


and also you add them as elements of the array to your container
I did it many times this way - very conveniently
Avatar of snajalm
snajalm

ASKER

You think this would be the best way and the only way??!
In general you can probably indeed create invidual namesdynamically  and even say instantite them using
Relection API:
http://download.oracle.com/javase/tutorial/reflect/
but unless you have some very special needs - you don;t want to do it - that will be  a big overhead an inconvenience - array is much simpler
Definitely would be the only way that peopele normally use in such situation.
You can also use ArrayList of JTextArea's - say if you may change theire number in the process -
but again this is not very typical - array of JTextArea (or other elements) is much more typical
Avatar of snajalm

ASKER

Thanks for_yan for your kind help!
You are always welcome.