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 8989001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:08:44+00:00 2026-06-15T22:08:44+00:00

I’m developing an application on my Java classes and hit a wall with a

  • 0

I’m developing an application on my Java classes and hit a wall with a strange issue. I need to represent data in a grid, so using GridLayout is an obvious choice, but here’s a problem. I keep on getting almost empty frame (notice tiny white rectangle in top left corner).

issue

Here’s a code snippet producing this result

//not important class code 
public static void main(String args[]) {    
    JFrame frame = new JFrame("Wolves & Rabbits");
    frame.setSize(640, 480);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //want to create a 12x9 grid with 2 black and 4 pink rectangles
    Board board = new Board(12, 9, 2, 4, 1000);

    frame.add(board);

    frame.setResizable(false);
    frame.setVisible(true);
}

//Board.java (Board class) extends JPanel
public JPanel fields[][];
private Integer boardWidth, boardHeight;
private ArrayList<AnimalThread> animals;
private Integer wolvesCount, rabbitsCount;

public Board(int w, int h) {
    super(new GridLayout(h, w, 4, 4));

    fields = new JPanel[w][h];
    boardWidth = new Integer(w);
    boardHeight = new Integer(h);
    animals = null;
    wolvesCount = new Integer(0);
    rabbitsCount = new Integer(0);

    //creating white rectangles
    for (int i = 0; i < boardHeight; i++)
        for (int j = 0; j < boardWidth; j++) {
            fields[j][i] = new JPanel(true);
            fields[j][i].setBackground(AnimalThread.NONE);
            this.add(fields[j][i]);
        }

    AnimalThread.setLinkToBoard(this);
}

public Board(int w, int h, int wolves, int rabbits, int k) {
    this(w, h);

    animals = new ArrayList<AnimalThread>();

    while (boardWidth*boardHeight < 2*wolves*rabbits) {
        wolves--;
        rabbits--;
    }       
    wolvesCount = wolves;
    rabbitsCount = rabbits;

    WolfThread.setRabbitsCount(rabbitsCount);

    //randomly place colored rectangles
    this.randomize(wolves, rabbits, k);     
}

The strange thing is that not changing Board class at all and with a little change in main method I was able to display the proper grid.

issue resolved

In this case the main method is

//not important class code
public static void main(String args[]) {    
    JFrame frame = new JFrame("Wolves & Rabbits");
    frame.setSize(640, 480);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Board board = new Board(12, 9, 2, 4, 1000);

    //THE CHANGE!
    JPanel panel = new JPanel(new GridLayout(12, 9, 4, 4));
    for (int i = 0; i < 9; i++)
        for (int j = 0; j < 12; j++) {
            JPanel tmp = board.fields[j][i];
            panel.add(tmp);
        }
    frame.add(panel);

    frame.setResizable(false);
    frame.setVisible(true);
}

Anyone has idea of what’s causing this irritating issue? Any clue would be appreciated.

  • 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-06-15T22:08:45+00:00Added an answer on June 15, 2026 at 10:08 pm

    When you are working with Swing you must execute the Swing UI code on the EDT. So at the very least your main method should look like this:

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Wolves & Rabbits");
                frame.setSize(640, 480);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
                // want to create a 12x9 grid with 2 black and 4 pink rectangles
                Board board = new Board(12, 9, 2, 4, 1000);
    
                frame.add(board);
    
                frame.setResizable(false);
                frame.setVisible(true);
            }
        });
    }
    

    Accessing variables which are Swing objects from an other thread then the EDT will cause problems. Many of these problems will be intermittent and hard to trace (like most concurrency problems).

    The name of the ‘AnimalThread’ object seems to imply its a Thread. You can’t (well actually you can as you demonstrated 🙂 directly Swing objects which ‘live’ on the EDT. If another Thread wants to change something on the EDT it needs to use the ‘SwingUtilities.invokeLater’ or ‘SwingUtilities.invokeAndWait’ method.

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

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

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.