Naming objects from string array

Hi,

I am trying to create some jlables and jtextfields using a for loop and a couple of arrays holding the desired names for these objects.

String[] labels = { "Name", "Address", "Address Two", "County", "Postcode", "Email", "Phone Number" };
String[] input = { "iName", "iAddress", "iAddress2", "iCounty", "iPostcode", "iEmail", "iPhone" };
int numRows = labels.length;
          
FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
setLayout(flow);
          
for (int i=0; i<numRows; i++) {
JLabel labels[i] = new JLabel(labels[i]);
add.(labels[i]);
}

Problem is it doesnt appear to like "JLabel lables[i]".  Am I not allowed to create objects like this or am I just making a silly mistake ?

Thanks.
LVL 14
Vel EousResearch & Development ManagerAsked:
Who is Participating?
 
objectsCommented:
String[] labelStrings = { "Name", "Address", "Address Two", "County", "Postcode", "Email", "Phone Number" };
String[] input = { "iName", "iAddress", "iAddress2", "iCounty", "iPostcode", "iEmail", "iPhone" };
int numRows = labels.length;
JLabel[] labels = new JLabel[labelStrings.length];
         
FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
setLayout(flow);
         
for (int i=0; i<numRows; i++) {
labels[i] = new JLabel(labelStrings[i]);
add.(labels[i]);
}
0
 
tbeernotCommented:
You are trying to assing the JLabel back to the array.

JLabel labels[i] = new JLabel(labels[i]);

Try

add(new JLabel(labels[i]));
0
 
tbeernotCommented:
public static void main(String[] args)
{
      try
      {
            String[] labels = { "Name", "Address", "Address Two", "County", "Postcode", "Email", "Phone Number" };
            String[] input = { "iName", "iAddress", "iAddress2", "iCounty", "iPostcode", "iEmail", "iPhone" };
            int numRows = labels.length;
                      
            JFrame f = new JFrame();
            FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
            f.setLayout(flow);
                            
            for (int i=0; i<numRows; i++) {
            f.add(new JLabel(labels[i]));
            }
                  
            f.pack();
            f.setVisible(true);
                  
      }
      catch (Throwable t)
      {
            t.printStackTrace();
      }
}
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Vel EousResearch & Development ManagerAuthor Commented:
Thanks for the suggestions folks, but its not the putting the object to the screen thats the issue.  I cannot work out how to name the object using the array element.  I need to be able to access my objects via their names at a later date.

// object obj_name = new object()
JLabel labels[i] = new JLabel();
0
 
tbeernotCommented:
ah, eh

JLabel x;
x.setName("blabla")?

0
 
tbeernotCommented:
Or use a map (assuming 1.5+)?

Map<String, JLabel> labels = new HashMap<String, JLabel>();
labels.put("blabla", new JLabel());
...
labels.get("blabla")...;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.