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??!
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??!
You would be better off if you have an array of JTextArea
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Somthing like this:
and also you add them as elements of the array to your container
I did it many times this way - very conveniently
JTextArea[] areas = new JTextArea[arraylist.size()];
for(int j=0; j<areas.length; j++){
area[j] = new JTextArea(20);
}
and also you add them as elements of the array to your container
I did it many times this way - very conveniently
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
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
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
ASKER
Thanks for_yan for your kind help!
You are always welcome.