Results 1 to 3 of 3
  1. #1
    chk
    chk is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default 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)
    Last edited by chk; 07-24-2010 at 12:02 AM. Reason: Adding Stack Trace

  2. #2
    chk
    chk is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    HaHa! I found the problem. Please disregard this post.

  3. #3
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    hey post the full code of the function... i'll correct it...the problem is not as u think..

Similar Threads

  1. Having difficulty adding graphics to a JPanel
    By DrKilljoy in forum New To Java
    Replies: 20
    Last Post: 07-20-2010, 08:40 PM
  2. Problem on adding JButton on JPanel NEED HELP
    By boisk in forum AWT / Swing
    Replies: 15
    Last Post: 03-15-2009, 02:27 PM
  3. How do I sent font of labels in an array?
    By Exhonour in forum New To Java
    Replies: 9
    Last Post: 01-14-2009, 03:48 PM
  4. adding a jpanel in the middle of the script
    By 2o2 in forum AWT / Swing
    Replies: 11
    Last Post: 10-12-2008, 05:50 PM
  5. Replies: 1
    Last Post: 07-04-2008, 05:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •