• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

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.
0
Vel Eous
Asked:
Vel Eous
  • 4
1 Solution
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now