[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Dynamically generated object names!

Posted on 2011-10-31
8
Medium Priority
?
386 Views
Last Modified: 2012-05-12
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??!
0
Comment
Question by:snajalm
  • 6
  • 2
8 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37060556
You would be better off if you have an array of JTextArea

0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 37060558
JTextArea[] areas = new JTextArea[arraylist.size()];

and then you ad aaddress them area[0], area[1], area[2], etc.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37060563
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:snajalm
ID: 37060601
You think this would be the best way and the only way??!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37060603
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
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37060605
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
0
 

Author Closing Comment

by:snajalm
ID: 37060650
Thanks for_yan for your kind help!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37060652
You are always welcome.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month20 days, 7 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question