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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:09:35+00:00 2026-06-15T06:09:35+00:00

I would like to layout components as shown in the picture only using the

  • 0

I would like to layout components as shown in the picture only using the GridBagLayout.
I have tried several constraints but it never end up with the expected result so I come to wonder if it is truly possible with only the GridBagLayout. The difficulty is for the C1, C2 and C3 components.
C1 and C2 are JComponent that will hold other components inside like JPanel. I have set their minimum and preferred size. C3 is a JButton.
C1 should not take extra space so i set its weightx to 0 and gridwidth to 1 (tried also with 2 as it span on C2 and C3).
C2 takes all extra space, i set its weightx to 1 and gridwidth to 3.
The GUI is not resizable.
I have use this LayoutManager several times but still don’t master it, thank you for a little help.

GridBagLayout

  • 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-15T06:09:35+00:00Added an answer on June 15, 2026 at 6:09 am
    • I’ll talking only about GridBagLayout, even this could be job exactly for MigLayout (MigLayout has additional parameter for fills numbers of Columns & Rows, resize, e.i.), and/or TableLayout(???)

    • GridBagLayout required only filling all desired numbers of columns in first row(only), then matrix is created and you can to define whatever GBC weightx, weighty, gridx, gridy and/or with Anchor too

    • example talking about

    enter image description here

    import java.awt.Color;
    import java.awt.ComponentOrientation;
    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;
    
    public class GbcLayout {
    
        private JFrame frame = new JFrame("GbcLayoutGbcLayout");
        private JPanel panel = new JPanel();
        private JLabel hidelLabel;
        private JLabel firstLabel;
        private JTextField firstText;
    
        public GbcLayout() {
            panel.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
            for (int k = 0; k < 50; k++) {
                hidelLabel = new JLabel("     ");
                hidelLabel.setOpaque(true);
                hidelLabel.setBackground(Color.orange);
                hidelLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.weightx = 0.5;
                gbc.weighty = 0.5;
                gbc.gridx = k;
                gbc.gridy = 0;
                panel.add(hidelLabel, gbc);
            }
            for (int k = 0; k < 5; k++) {
                firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT);
                firstLabel.setFont(new Font("Serif", Font.BOLD, 20));
                firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.insets = new Insets(0, 0, 5, 0);
                gbc.gridx = 0;
                gbc.gridwidth = 8;
                gbc.gridy = k + 1;
                panel.add(firstLabel, gbc);
            }
            for (int k = 0; k < 5; k++) {
                firstText = new JTextField("Testing TextField");
                firstText.setFont(new Font("Serif", Font.BOLD, 20));
                firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.insets = new Insets(0, 0, 5, 0);
                gbc.gridx = 9;
                gbc.gridwidth = k + 8;
                gbc.gridy = k + 1;
                panel.add(firstText, gbc);
            }
            for (int k = 0; k < 5; k++) {
                firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT);
                firstLabel.setFont(new Font("Serif", Font.BOLD, 20));
                firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.insets = new Insets(0, 0, 5, 0);
                gbc.gridx = 20 + k;
                gbc.gridwidth = 8;
                gbc.gridy = k + 1;
                panel.add(firstLabel, gbc);
            }
            for (int k = 0; k < 5; k++) {
                firstText = new JTextField("Testing TextField");
                firstText.setFont(new Font("Serif", Font.BOLD, 20));
                firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.insets = new Insets(0, 0, 5, 0);
                gbc.gridx = 29 + k;
                gbc.gridwidth = 21 - k;
                gbc.gridy = k + 1;
                panel.add(firstText, gbc);
            }
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(panel);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
    
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    GbcLayout gbcl = new GbcLayout();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have reusable ratings (typical layout with 5 stars). I have
I have a reasonably complex layout problem: I would like to have a main
I have a quite complicated HTML/CSS layout which I would like to convert to
I have a two column layout: http://jsfiddle.net/KqQ42/1/ Now I would like that the left
I would like to create an custom View on Android. I have tried to
I would like to create this layout in Silverlight. The text need to wrap
I would like to create a simple fluid layout whereby 4 columns of Equal
I would like to disable address space layout randomization (ASLR) on my system (Ubuntu
I would like to use CSS to present a two-column layout. The markup I
I would like ask if there's a way to download an android layout from

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.