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)
Quote:
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)
{
}
}
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.
Re: Problem with adding an arraylist of jbuttons to a jpanel
Quote:
Originally Posted by
Norm
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 :cool:
Re: Problem with adding an arraylist of jbuttons to a jpanel
Quote:
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.