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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:12:25+00:00 2026-06-16T21:12:25+00:00

Possible Duplicate: Alignment issue in GridBagLayout Please have a look at the following code

  • 0

Possible Duplicate:
Alignment issue in GridBagLayout

Please have a look at the following code

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

public class TestForm extends JFrame
{
    private JLabel firstLabel, secondLabel, thirdLabel, fourthLabel, fifthLabel;

    private JTextField firstTxt, secondTxt, thirdTxt, fourthTxt, fifthTxt;

    private JPanel centerPanel;
    private JPanel southPanel;
    private JLabel comboLabel;
    private JComboBox percentageCombo;
    private JLabel endTargetLabel;
    private JLabel mustLoseLabel;

    private GridBagLayout mainLayout = new GridBagLayout();
    private GridBagConstraints mainCons = new GridBagConstraints();

      public TestForm()
      {
        //Declaring instance variables  
        firstLabel = new JLabel("First: ");
        secondLabel = new JLabel("Second: ");
        thirdLabel = new JLabel("Third: ");
        fourthLabel = new JLabel("Fourth: ");
        fifthLabel = new JLabel("Fifth: ");        
        comboLabel = new JLabel("Select System Performance: ");

        firstTxt = new JTextField(7);
        secondTxt = new JTextField(7);
        thirdTxt = new JTextField(7);
        fourthTxt = new JTextField(7);
        fifthTxt = new JTextField(7);

        endTargetLabel = new JLabel("Your End Target Performance is: ");
        mustLoseLabel = new JLabel("Sammple Performance You Must Lose: ");  

        percentageCombo = new JComboBox();
        percentageCombo.addItem("No Value is Set");


       this.setLayout(mainLayout);
        mainCons.gridy = 1;
        mainCons.gridx = 1;
        mainCons.anchor = GridBagConstraints.NORTH;
        this.add(createNorthPanel(),mainCons);

        mainCons.anchor = GridBagConstraints.WEST;
        mainCons.gridy = 2;
        mainCons.gridx = 1;
        mainCons.anchor = GridBagConstraints.CENTER;
        mainCons.insets = new Insets(15,0,0,0);
        this.add(createCenterPanel(),mainCons);

        mainCons.anchor = GridBagConstraints.SOUTH;
        mainCons.gridy = 3;
        mainCons.gridx = 1;
        this.add(createSouthPanel(),mainCons);

        this.setTitle("The Test Form");
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

    private JPanel createNorthPanel()
    {
        JPanel northPanel = new JPanel();

        northPanel = new JPanel();
        northPanel.setLayout(new FlowLayout());

        JLabel logoLabel = new JLabel();
        logoLabel.setIcon(new ImageIcon(getClass().getResource("/images/TESTING-LOGO.gif")));

        northPanel.add(logoLabel);

        return northPanel;
    }


    private JPanel createCenterPanel()
    {
        centerPanel = new JPanel();

        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();

        centerPanel.setLayout(gbl);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,0,0,0);
        centerPanel.add(firstLabel,gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,0,0,0);
        centerPanel.add(firstTxt,gbc);

        gbc.gridx = 3;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,10,0,0);
        centerPanel.add(secondLabel,gbc);

        gbc.gridx = 4;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,-10,0,0);
        centerPanel.add(secondTxt,gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,0,0,0);
        centerPanel.add(thirdLabel,gbc);

        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,0,0,0);
        centerPanel.add(thirdTxt,gbc);

        gbc.gridx = 3;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,10,0,0);
        centerPanel.add(fourthLabel,gbc);

        gbc.gridx = 4;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,-10,0,0);
        centerPanel.add(fourthTxt,gbc);

        gbc.gridx = 5;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,7,0,0);
        centerPanel.add(fifthLabel,gbc);

        gbc.gridx = 6;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(fifthTxt,gbc);

        centerPanel.setBorder(BorderFactory.createTitledBorder("The Testing Form"));
        centerPanel.validate();

        return centerPanel;

    }


     private JPanel createSouthPanel()
    {
        southPanel = new JPanel();

        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();

        southPanel.setLayout(gbl);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,0,0,0);
        southPanel.add(comboLabel,gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        southPanel.add(percentageCombo,gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(10,0,0,0);
        southPanel.add(endTargetLabel,gbc);


        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(10,0,0,0);
        southPanel.add(mustLoseLabel,gbc);

        southPanel.setBorder(BorderFactory.createTitledBorder("See Your End Target "));

        return southPanel;
    }

    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new TestForm();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

This is what I get when I use this code

enter image description here

But, I need the following

  1. Both southPanel and centerPanel should appear in a one vertical line. In here, the southPanel is not in the same line as centerPanel, it is bit more aligned to the right
  2. Both Panels should move left a bit (showed in image)
  3. Both Panels should have the same size
  4. The size of the panels are too small. They should be bit large than now, while not changing the spaces and alignments to its components.

enter image description here

If you know the answer for at least one question, please help. I am attaching the logo as well.

enter image description here

Note: Removing more elements from the code will not generate the original issue. That is why this code is bit big.

  • 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-16T21:12:27+00:00Added an answer on June 16, 2026 at 9:12 pm

    You need to assign more “weight” to each of the panels, which will force them to align within the given space.

    enter image description here

    this.setLayout(mainLayout);
    mainCons.gridy = 1;
    mainCons.gridx = 1;
    mainCons.anchor = GridBagConstraints.NORTHWEST;
    mainCons.weightx = 1;
    this.add(createNorthPanel(), mainCons);
    
    mainCons.anchor = GridBagConstraints.WEST;
    mainCons.weightx = 1;
    mainCons.gridy = 2;
    mainCons.gridx = 1;
    mainCons.insets = new Insets(15, 0, 0, 0);
    this.add(createCenterPanel(), mainCons);
    
    mainCons.anchor = GridBagConstraints.SOUTHWEST;
    mainCons.weightx = 1;
    mainCons.gridy = 3;
    mainCons.gridx = 1;
    this.add(createSouthPanel(), mainCons);
    

    As a side node, you can actually use the same instance of the GridBagConstraints and change only those values you need, which make you code a little easier to read (not to mention reduce the amount you need to type ;))

    You might like to take a look at How to use GridBagLayout

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

Sidebar

Related Questions

Possible Duplicate: Shortcut “or-assignment” (|=) operator in Java I found the following example code
Possible Duplicate: Java, GUI builder or hand coding? I have been making GUI hand-coded
Possible Duplicate: Combine two Images into one new Image I have a c# code
Possible Duplicate: Conditional operator assignment with Nullable<value> types? in the following code snippet company.ParentID
Possible Duplicate: JSONkit sorting issues I have the following jSON [ { Pending: 67,
Possible Duplicate: C++ alignment when printing cout << I am writing a set of
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: Javascript multiple assignment statement I was wondering what this does. I have
Possible Duplicate: Modify the function variables frominner function in python Say I have this
Possible Duplicate: Assignment from incompatible pointer type warning I have two structures, one which

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.