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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:30:39+00:00 2026-05-26T02:30:39+00:00

I’m trying to display a frame using GridLayout, but one of my panels isn’t

  • 0

I’m trying to display a frame using GridLayout, but one of my panels isn’t displaying.

The JPanel that I’m having trouble with (gridPanel) is supposed to have a 50 by 50 GridLayout and each cell in that grid is supposed to have a Square object added to it. Then that panel is supposed to be added to the frame, but it doesn’t display.

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

public class Gui extends JFrame{

    JPanel buttonPanel, populationPanel, velocityPanel, gridPanel;
    JButton setupButton, stepButton, goButton;
    JLabel populationLabel, velocityLabel;
    JSlider populationSlider, velocitySlider;
    Square [] [] square;

    public Gui() {

        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        //Set up JButtons
        buttonPanel = new JPanel();
        setupButton = new JButton("Setup");
        stepButton = new JButton("Step");
        goButton = new JButton("Go");

        buttonPanel.add(setupButton);
        buttonPanel.add(stepButton);
        buttonPanel.add(goButton);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 2;   //2 columns wide
        add(buttonPanel, c);


        //Set up populationPanel    
        populationPanel = new JPanel();
        populationLabel = new JLabel("Population");
        populationSlider = new JSlider(JSlider.HORIZONTAL,0, 200, 1);

        populationPanel.add(populationLabel);
        populationPanel.add(populationSlider);

        c.fill = GridBagConstraints.LAST_LINE_END;
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 2;   //2 columns wide
        add(populationPanel, c);        


        //Set up velocityPanel
        velocityPanel = new JPanel();
        velocityLabel = new JLabel("    Velocity");
        velocitySlider = new JSlider(JSlider.HORIZONTAL,0, 200, 1);

        velocityPanel.add(velocityLabel);
        velocityPanel.add(velocitySlider);

        c.fill = GridBagConstraints.LAST_LINE_END;
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 2;   //2 columns wide
        add(velocityPanel, c);  


        //Set up gridPanel
        JPanel gridPanel = new JPanel(new GridLayout(50, 50));
        square = new Square[50][50];

        for(int i = 0; i < 50; i++){
            for(int j = 0; j < 50; j++){
                    square[i][j] = new Square();
                    gridPanel.add(square[i][j]);
            }
        }

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;   //2 columns wide
        add(gridPanel, c);  
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        Gui frame = new Gui();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Square class

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

public class Square extends JComponent{

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.fillRect(10, 10, 10, 10);
    }

}
  • 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-26T02:30:40+00:00Added an answer on May 26, 2026 at 2:30 am

    The reason you are not seeing anything are two-fold (though not entirely sure if the second is intentional, guessing on that part 🙂

    • JComponent is a bare-bone container, it has no ui delegate and no inherent size – it’s up to your code to return something reasonable for min/max/pref
    • the painting rect is hard-coded to “somewhere” inside, which might or might be inside the actual component area

    Following is showing (border just to see the boundaries)

    public static class Square extends JComponent {
    
        public Square() {
            setBorder(BorderFactory.createLineBorder(Color.RED));
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, getWidth(), getHeight());
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(10, 10);
        }
        @Override
        public Dimension getMaximumSize() {
            return getPreferredSize();
        }
        @Override
        public Dimension getMinimumSize() {
            return getPreferredSize();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
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
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

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.