title: Grid of Buttons ActionListener
body: <pre><code>public ButtonGrid(int width, int length){
          Random r=new Random();
          int w=r.nextInt(13-1)+1;
          JTextField g = new JTextField();
          Scanner u=new Scanner(System.in);
          frame.setSize(500, 500);
          frame.setLayout(new GridLayout(width,length));
          grid=new JButton[width][length];
          for(y=0;y&lt;length;y++){
              for(x=0;x&lt;width;x++){
                  //if (y &lt; 4) {
                      //grid[x][y]=new JButton("x");
                  //} 
                  //else if (y&gt;5){ 
                      //grid[x][y]=new JButton(""+u.nextInt());
                      //frame.setVisible(true);;
                  //}
                  //else{
                      grid[x][y]=new JButton(" ");
                  //}
                  frame.add(grid[x][y]);
              }
          }
          grid[x][y].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  System.out.println("Hello");
                  ((JButton)e.getSource()).setBackground(Color.red);
              }
          });
  </code></pre>
  
  <p>I have a grid of buttons and when I try to add an actionListener it gives my an error saying OutOfBoundsException 
  I just wanted it to be so when I click any button it will print hello and it will turn red.
  Please Help</p>