Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem with adding an arraylist of jbuttons to a jpanel

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Problem with adding an arraylist of jbuttons to a jpanel

    Hello, I'm having a problem with the following code where I keep getting the following error:

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 9, Size: 9
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at MoleGUI.<init>(MoleGUI.java:45)
    at MoleMain.main(MoleMain.java:6)


    Can anyone look at my code and tell me what the problem is please?
    Btw I do have a main class which can run this (just saying lol)

    import javax.swing.*;
    import javax.swing.Timer;
    import java.util.*;
    import java.awt.event.*;
    import java.awt.*;

    public class MoleGUI implements ActionListener{

    JFrame field;
    JPanel panel;
    JButton h1,h2,h3,h4,h5,h6,h7,h8,h9;
    Timer t = new Timer(5, this);
    int counter;
    ArrayList<JButton> holes;


    public MoleGUI() {
    field = new JFrame("Whack-a-Mole");
    panel = new JPanel(new GridLayout(3,3));
    field.setContentPane(panel);
    field.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    holes = new ArrayList<JButton>();

    holes.add(h1 = new JButton());
    holes.add(h2 = new JButton());
    holes.add(h3 = new JButton());
    holes.add(h4 = new JButton());
    holes.add(h5 = new JButton());
    holes.add(h6 = new JButton());
    holes.add(h7 = new JButton());
    holes.add(h8 = new JButton());
    holes.add(h9 = new JButton());

    panel.add(holes.get(1));
    panel.add(holes.get(2));
    panel.add(holes.get(3));
    panel.add(holes.get(4));
    panel.add(holes.get(5));
    panel.add(holes.get(6));
    panel.add(holes.get(7));
    panel.add(holes.get(8));
    panel.add(holes.get(9));

    holes.get(1).addActionListener(this);
    holes.get(2).addActionListener(this);
    holes.get(3).addActionListener(this);
    holes.get(4).addActionListener(this);
    holes.get(5).addActionListener(this);
    holes.get(6).addActionListener(this);
    holes.get(7).addActionListener(this);
    holes.get(8).addActionListener(this);
    holes.get(9).addActionListener(this);

    //counter = 0;
    //t.start();
    }

    public void display(){
    field.pack();
    field.setVisible(true);
    field.resize(500,500);
    //run();
    }

    public void run()
    {

    }

    public void actionPerformed(ActionEvent e)
    {

    }

    }
    Last edited by jm24; February 8th, 2012 at 10:46 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with adding an arraylist of jbuttons to a jpanel

    Array indexes are zero based. If there are 9 elements in an array, the max index is 8 (the length-1)

    You should look at using a for loop controlled by the size of the array instead of hardcoding all those separate indexing statements.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Problem with adding an arraylist of jbuttons to a jpanel

    Quote Originally Posted by Norm View Post
    Array indexes are zero based. If there are 9 elements in an array, the max index is 8 (the length-1)

    You should look at using a for loop controlled by the size of the array instead of hardcoding all those separate indexing statements.
    Haha wow it's funny because I knew that too. I guess I just didn't catch the error in the code. Thanks for the help

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Problem with adding an arraylist of jbuttons to a jpanel

    Haha wow it's funny because I knew that too. I guess I just didn't catch the error in the code. Thanks for the help
    And don't forget to thanks or hit the Rep button (Star) when someone helps you. Good Luck and mark this as Solved.

Similar Threads

  1. Somehow it's not adding to an ArrayList of ArrayLists.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 28th, 2012, 05:09 PM
  2. Help with ArrayList ( Adding certain elements together)
    By williamsant in forum Collections and Generics
    Replies: 13
    Last Post: September 27th, 2011, 09:32 AM
  3. [SOLVED] Adding Scrollpane to jPanel
    By Onur in forum AWT / Java Swing
    Replies: 5
    Last Post: August 25th, 2011, 03:04 PM
  4. Adding JButtons to Frame
    By iKlaush in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 12th, 2011, 10:52 AM
  5. [SOLVED] What is not right here? adding JPanel in JFrame
    By Asido in forum AWT / Java Swing
    Replies: 2
    Last Post: August 23rd, 2010, 08:16 AM