Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 786443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:02:31+00:00 2026-05-14T21:02:31+00:00

I’m working through a JPanel exercise in a Java book. I’m tasked with creating

  • 0

I’m working through a JPanel exercise in a Java book. I’m tasked with creating a 5×4 grid using GridLayout.

When I loop through the container to add panels and buttons, the first add() throws the OOB exception. What am I doing wrong?

package mineField;

import java.awt.*;
import javax.swing.*;

@SuppressWarnings("serial")

public class MineField extends JFrame {

private final int WIDTH = 250;
private final int HEIGHT = 120;
private final int MAX_ROWS = 5; 
private final int MAX_COLUMNS = 4;

public MineField() {

    super("Minefield");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container mineFieldGrid = getContentPane();
    mineFieldGrid.setLayout(new GridLayout(MAX_ROWS, MAX_COLUMNS));

    // loop through arrays, add panels, then add buttons to panels.
    for (int i = 0; i < MAX_ROWS; i++) {
        JPanel[] rows = new JPanel[i];

        mineFieldGrid.add(rows[i], rows[i].getName());
        rows[i].setBackground(Color.blue);

        for (int j = 0; j < MAX_COLUMNS; j++) {
            JButton[] buttons = new JButton[i];             
            rows[i].add(buttons[j], buttons[j].getName());
        }
    }

    mineFieldGrid.setSize(WIDTH, HEIGHT);
    mineFieldGrid.setVisible(true);

}

public int setRandomBomb(Container con)
{

    int bombID;

    bombID = (int) (Math.random() * con.getComponentCount());
    return bombID;


}
/**
 * @param args
 */
public static void main(String[] args) {

    //int randomBomb;
    //JButton bombLocation;

    MineField minePanel = new MineField();
    //minePanel[randomBomb] = minePanel.setRandomBomb(minePanel);

}

}

I’m sure I’m over-engineering a simple nested for loop. Since I’m new to Java, please be kind. I’m sure I’ll return the favor some day.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:02:32+00:00Added an answer on May 14, 2026 at 9:02 pm

    JPanel[] rows = new JPanel[i];

    i is 0 in the first iteration, which isn’t what you want. Make that:

    JPanel[] rows = new JPanel[MAX_ROWS];

    Also, I think you want to take that completely outside the for loop, since you seem to be using its elements, which would be uninitialised…

    This is also wrong:

    JButton[] buttons = new JButton[i];

    i can be 0 when j is 2 for example, in which case there’s no such thing as a buttons[j]. Make them all MAX_* and I think you want to take them out of the loop, since I don’t see the point in recreating them at every iteration. Also, you need to instantiate the individual array elements as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 401k
  • Answers 401k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should take a look at http://json.codeplex.com/ which would allow… May 15, 2026 at 4:39 am
  • Editorial Team
    Editorial Team added an answer Can another case label come after the default clause? Yes,… May 15, 2026 at 4:39 am
  • Editorial Team
    Editorial Team added an answer In C++ you don't have to give a parameter that… May 15, 2026 at 4:39 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.