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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:41:27+00:00 2026-06-16T11:41:27+00:00

I have a very simple Java program (see below). The GridLayout has 20 rows

  • 0

I have a very simple Java program (see below). The GridLayout has 20 rows and 4 columns. As you know the elements are supposed to be added horizontally by (GridLayout) definition. However, I get the two elements (labels) placed one above the other, vertically.

I colored them and realised the labels take up the whole row, hence the vertical effect. But then I also used setSize(5,5) with each to make them smaller, however they still take up the whole row. Any advice as to why this happens and how to fix/set smaller size/etc?

public class Sam extends JFrame {

    public JButton btn_arr;
    public Container c;
    public JLabel[] lbl = new JLabel[20];


    public Sam()
    {
        c = getContentPane();
        c.setLayout(new GridLayout(20,4));
        lbl[1] = new JLabel("Column1");
        c.add(lbl[1]);

        lbl[2] = new JLabel("Column2");
        c.add(lbl[2]);

        show();     
    }

    public static void main(String[] args) 
    {
        Sam x = new Sam();
        x.setVisible(true);
        x.setSize(7500,4500);
    }

}
  • 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-16T11:41:28+00:00Added an answer on June 16, 2026 at 11:41 am

    You’re only adding two components to the grid so they will fill it up. You need to add more components to the grid as placeholders so that it can place the original JLabels in their proper place, perhaps empty JLabels or JPanels.

    As an aside, you should avoid setting the size of any Swing component. Your current size of 7500, 4500 is a bit on the large size.

    As a second aside, perhaps you want to use a JTable instead here.

    Edit: if you want a GridLayout with 4 columns and variable number of rows, use 0 for your GridLayout row constant:

    c.setLayout(new GridLayout(0, 4));
    

    e.g.,

    import java.awt.*;
    import javax.swing.*;
    
    public class Sam extends JFrame {
       public static final int COLUMN_COUNT = 4;
    
       public JButton btn_arr;
       public Container c;
       public JLabel[] lbl = new JLabel[COLUMN_COUNT];
    
       public Sam() {
          c = getContentPane();
          c.setLayout(new GridLayout(0, COLUMN_COUNT));
    
          for (int i = 0; i < lbl.length; i++) {
             lbl[i] = new JLabel("Column " + (i + 1));
             c.add(lbl[i]);
          }
    
       }
    
       public static void main(String[] args) {
          Sam x = new Sam();
          x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          x.pack();
          x.setLocationRelativeTo(null);
          x.setVisible(true);
          // x.setSize(7500,4500);
       }
    
    }
    

    But still I wonder if a JTable wouldn’t work better here.

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

Sidebar

Related Questions

I have a very simple question. How can I export Java program like this
I have written the following very simple Java program to ask user enter a
I have a very simple java program that prints out 1 million random numbers.
I have a very simple highcharts js chart, which has dates on x-axis and
I write a very simple java program with two classes: Business and Main .
I have a very simple command line Java application that I wish to port
I noticed that when I build even a very simple Java program in Eclipse,
I have an assignment in my java class to make a very simple phishing
I have a very simple program, that loads an wavefront obj file, rotate and
I have a very simple program with one textbox, and a button. The user

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.