Please Help. Adding array of labels and fields to JPanel
Hi. I am new to java and am hoping someone can help with this. I have an array of JLabels and an array of JTextFields. I use a for loop to add them to the panel and everything works fine, but when I only want to add the labels and fields starting at index[1] and going to the end of the array, I get an ArrayOutofBounds-No Such Child error. To do this I initialize the index to 1. What am I missing? Is there a better way to do this? The program is pretty long so I don't think I can post all the code. Here are my loops that create the labels and fields and adds them to the panel. If you need more of my code I can post it. Thanks.
for(int i=0; i < fields.length;i++)
{
fields[i] = new JTextField();
fields[i].setColumns(20);
fields[i].addActionListener(this);
}
for (int i = 0; i < fields.length; i++)
{
labels[i] = new JLabel(labelStrings[i],
JLabel.TRAILING);
labels[i].setLabelFor(fields[i]);
}
for(int i =1;i<fields.length;i++)
{
panel.add(labels[i]);
panel.add(fields[i]);
}
Here is the stack trace
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: No such child: 0
at java.awt.Container.getComponent(Container.java:294 )
at SpringUtilities.getConstraintsForCell(SpringUtilit ies.java:122)
at SpringUtilities.makeCompactGrid(SpringUtilities.ja va:158)
at FormInput.createEntryFields(FormInput.java:490)
at FormInput.<init>(FormInput.java:64)
at FormInput.createAndShowGUI(FormInput.java:527)
at FormInput.access$000(FormInput.java:11)
at FormInput$3.run(FormInput.java:545)
at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)