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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:14:47+00:00 2026-05-22T00:14:47+00:00

I am messing around with the GridBagLayout. I have understood it somewhat, hence was

  • 0

I am messing around with the GridBagLayout. I have understood it somewhat, hence was able to make this layout. but my As-Is is not matching with my should be. here are the screens.

As-Is 🙁
As-Is :(

Should Be :s
Should Be :s

I understand that i have to tweak it a bit so that the size is set (setSize()). But the real tricky one is getting the “Add Contact” JLabel to be in the center at the top.
Waiting for your replies. Thanks in Advance.

Here’s My Code

package SimpleCRUD;

import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class ContactListFrame extends JFrame{
    JButton Button1, Button2;
    JTextField textField1, textField2, textField3;
    JLabel label1, label2, label3 , label4;
    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints Constraint = new GridBagConstraints();

    public ContactListFrame() {
        super("All Contacts");
        Button1 = new JButton("Add");
        Button2 = new JButton("Cancel");
        textField1 = new JTextField(15);
        textField2 = new JTextField(15);
        textField3 = new JTextField(15);
        label4 = new JLabel("Add Contact");
        label4.setFont (new Font("fallan", 1, 25));
        label1 = new JLabel("First Name:");
        label2 = new JLabel("Last Name:");
        label3 = new JLabel("Phone Number:");

        setLayout(layout);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setSize(400, 200);
        setResizable(false);
        Constraint.fill = GridBagConstraints.NONE;
        Constraint.anchor = GridBagConstraints.NORTH;
        addComponent(label4, 0, 1, 1, 1);
        addComponent(textField1, 1, 1, 1, 1);
        addComponent(textField2, 2, 1, 1, 1);
        addComponent(textField3, 3, 1, 1, 1);
        addComponent(label1, 1, 0, 1, 1);
        addComponent(label2, 2, 0, 1, 1);
        addComponent(label3, 3, 0, 1, 1);
        addComponent(Button1, 4, 0, 2, 1);
        addComponent(Button2, 4, 1, 2, 1);
    }

    public void addComponent (Component comp, int row, int col, int width, int height){
        Constraint.gridx = col;
        Constraint.gridy = row;
        Constraint.gridwidth = width;
        Constraint.gridheight = height;
        layout.setConstraints(comp, Constraint);
        add(comp);
    }

}
  • 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-22T00:14:48+00:00Added an answer on May 22, 2026 at 12:14 am

    If you take a look at the documentation of GridBagLayout, there’s a fairly good example of GridBagConstraints usage.

    Here’s your code modified to use GridBagConstraints

    import java.awt.Component;
    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    
    import java.awt.Insets;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    
    public class ContactListFrame extends JFrame{
        JButton Button1, Button2;
        JTextField textField1, textField2, textField3;
        JLabel label1, label2, label3 , label4;
        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints Constraint = new GridBagConstraints();
    
        public ContactListFrame() {
            super("All Contacts");
            Button1 = new JButton("Add");
            Button2 = new JButton("Cancel");
            textField1 = new JTextField(15);
            textField2 = new JTextField(15);
            textField3 = new JTextField(15);
            label4 = new JLabel("Add Contact");
            label4.setFont (new Font("fallan", 1, 25));
            label1 = new JLabel("First Name:");
            label2 = new JLabel("Last Name:");
            label3 = new JLabel("Phone Number:");
    
            setLayout(layout);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(400, 200);
            setResizable(false);
            Constraint.fill = GridBagConstraints.BOTH;
            Insets ins = new Insets(5, 5, 5, 5);
            Constraint.insets = ins;//this does the padding
            Constraint.weightx = 0.0;
            Constraint.gridwidth = GridBagConstraints.REMAINDER;//end row
            add(label4, Constraint);
            Constraint.gridwidth = GridBagConstraints.RELATIVE;//next to last component
            add(label1, Constraint);
            Constraint.gridwidth = GridBagConstraints.REMAINDER;//end row
            add(textField1, Constraint);
            Constraint.gridwidth = GridBagConstraints.RELATIVE;//next to last component
            add(label2, Constraint);
            Constraint.gridwidth = GridBagConstraints.REMAINDER;//end row
            add(textField2, Constraint);
            Constraint.gridwidth = GridBagConstraints.RELATIVE;//next to last component
            add(label3, Constraint);
            Constraint.gridwidth = GridBagConstraints.REMAINDER;//end row
            add(textField3, Constraint);
            Constraint.gridwidth = GridBagConstraints.RELATIVE;//next to last component
            add(Button1, Constraint);
            Constraint.gridwidth = GridBagConstraints.REMAINDER;//end row
            add(Button2, Constraint);
        }        
    
        public static void main(String args[]) {
          new ContactListFrame().setVisible(true);
        }
    }
    

    The output looks like this

    enter image description here

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

Sidebar

Related Questions

I'm messing around with expression trees, but I'm little stuck. I have this expression:
I've been messing around with this for a while now, but I can't seem
I have been messing around with Cocos2d for a while but decided that, due
I have been messing around with my meta descriptions, trying to make them more
I was messing around with RhinoMocks this morning and couldn't run my tests because
I am messing around with Doctrine (version 1.0.3) to see if would make a
Im just messing around with Ruby on Rails and HTML. This question isn't about
i have been messing around with a search engine and i am trying to
I was just messing around with templates, when I tried to do this: template<typename
I was messing around with JavaScript, and noticed that this can never be a

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.