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

  • Home
  • SEARCH
  • 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 7829873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:57:01+00:00 2026-06-02T10:57:01+00:00

Just playing around with Swing and wondering why the following code constructs a layout

  • 0

Just playing around with Swing and wondering why the following code constructs a layout that seems to have three columns when the GridLayout is defined as 10 rows and 10 columns?

Can anybody explain this unusual behaviour and what in the code provided is causing this to happen?

public class MyGrid {

    public static void main (String[] args) {
        JFrame frame = new JFrame();
        Container container = frame.getContentPane();
        container.setLayout(new GridLayout(10,10));
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if (i>=j) {
                    container.add(new JButton("X"));
                } else {
                    container.add(new JLabel("Y"));
                }
            }
        }
        frame.setSize(500,500);
        frame.setVisible(true);
    }

}
  • 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-02T10:57:03+00:00Added an answer on June 2, 2026 at 10:57 am

    See the class javadoc of GridLayout:

    When both the number of rows and the number of columns have been set to non-zero values, either by a constructor or by the setRows and setColumns methods, the number of columns specified is ignored. Instead, the number of columns is determined from the specified number of rows and the total number of components in the layout. So, for example, if three rows and two columns have been specified and nine components are added to the layout, they will be displayed as three rows of three columns. Specifying the number of columns affects the layout only when the number of rows is set to zero.

    If you use this code

    public class MyGrid {
    
      public static void main (String[] args) {
        JFrame frame = new JFrame();
        Container container = frame.getContentPane();
        container.setLayout(new GridLayout(10,10));
        for ( int i =0; i < 100; i++ ){
          container.add( new JLabel( ""+i ) );
        }
        frame.setSize(500,500);
        frame.setVisible(true);
      }
    
    }
    

    you would see 10 rows and 10 columns. If you use i < 50 for example in the for loop, the number of columns change.

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

Sidebar

Related Questions

I'm just playing around with some code but it seemed to have the specific
I have a very bad code written in my program, was just playing around
Just playing around with memcache for the first time; here's my code: $memcache =
I'm just playing around with WPF and MVVM, and I have made a simple
I'm just playing around with FastMember and have hit a problem. Each time I
I'm just playing around with PivotViewer control in Silverlight 5. It seems like many
I was just playing around with some code in LINQPad and managed to crash
I'm just playing around with some code. I create an Activity and simply do
I'm just playing around with some PHP and was wondering what happens when an
I'm just playing around with a simple program that opens the camera. That's literally

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.