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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:11:08+00:00 2026-06-02T02:11:08+00:00

import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class GridBagLayoutDemo extends JFrame {

  • 0
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class GridBagLayoutDemo extends JFrame
{
private JFrame frame;
private JPanel panel;
private JLabel l1,l2,l3,l4,l5,l6,l7,l8;
private JRadioButton r1,r2,r3,r4,r5,r6,r7,r8;
private JTextField t1,t2,t3,t4;
private JComboBox c1;
GridBagConstraints gbc;
GridBagLayout  gbl;


public static void main(String[] args)
{
    new GridBagLayoutDemo();
}

public GridBagLayoutDemo()
{
    frame=new JFrame();
    panel=new JPanel();
    panel.setBackground(Color.YELLOW);
    gbl   = new GridBagLayout();  

    gbc = new GridBagConstraints(); 

    panel.setLayout(gbl);

    l1=new JLabel("passport");
    gbc.anchor = gbc.EAST;  
    gbc.gridx=0;
    gbc.gridy=0;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    //gbc.weightx=1.0;
    panel.add(l1,gbc);

    l2=new JLabel("pass no.");
    gbc.gridx=0;
    gbc.gridy=1;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(l2,gbc);


    l3=new JLabel("Valid Upto");
    gbc.gridx=0;
    gbc.gridy=2;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(l3,gbc);

    l4=new JLabel("Identification");
    gbc.gridx=0;
    gbc.gridy=3;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(l4,gbc);

    l5=new JLabel("NO.");
    gbc.gridx=0;
    gbc.gridy=4;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(l5,gbc);

    l6=new JLabel("Marital Status");
    gbc.gridx=0;
    gbc.gridy=5;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(l6,gbc);

    l7=new JLabel("Nationality");
    gbc.gridx=0;
    gbc.gridy=6;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(l7,gbc);

    l8=new JLabel("Type of Position");
    gbc.gridx=0;
    gbc.gridy=7;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(l8,gbc);

    r1=new JRadioButton("Yes");
    gbc.gridx=2;
    gbc.gridy=0;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(r1,gbc);

    r2=new JRadioButton("No");
    gbc.gridx=4;
    gbc.gridy=0;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(r2,gbc);

    r3=new JRadioButton("Married");
    gbc.gridx=2;
    gbc.gridy=5;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(r3,gbc);

    r4=new JRadioButton("Unmarried");
    gbc.gridx=4;
    gbc.gridy=5;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(r4,gbc);

    r5=new JRadioButton("Full time");
    gbc.gridx=2;
    gbc.gridy=7;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(r5,gbc);

    r6=new JRadioButton("Part Time");
    gbc.gridx=3;
    gbc.gridy=7;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(r6,gbc);

    r7=new JRadioButton("Contract Basis");
    gbc.gridx=4;
    gbc.gridy=7;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(r7,gbc);

    r8=new JRadioButton("Intern");
    gbc.gridx=2;
    gbc.gridy=8;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(r8,gbc);


    t1=new JTextField();
    gbc.gridx=2;
    gbc.gridy=1;
    gbc.gridwidth=3;
    gbc.gridheight=1;
    panel.add(t1,gbc);

    t2=new JTextField();
    gbc.gridx=2;
    gbc.gridy=2;
    gbc.gridwidth=3;
    gbc.gridheight=1;
    panel.add(t2,gbc);

    t3=new JTextField();
    gbc.gridx=2;
    gbc.gridy=4;
    gbc.gridwidth=3;
    gbc.gridheight=1;
    panel.add(t3,gbc);

    t4=new JTextField();
    gbc.gridx=2;
    gbc.gridy=6;
    gbc.gridwidth=3;
    gbc.gridheight=1;
    panel.add(t4,gbc);





    frame.add(panel);

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    //frame.setSize(300,200);
    frame.pack();

    //c1=new JComboBox();
    //frame1();


}
}

In this I have added a few labels,radio buttons,textfields.The radio buttons and labels are getting displayed as I want them but the text fields are not getting displayed properly.
Can anyone tell me how to get the text fields to cover more space?

This is how the gui is displayed

  • 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-02T02:11:09+00:00Added an answer on June 2, 2026 at 2:11 am

    You should probably consider setting the two following values on your constraints for the textfields:

    GridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    GridBagConstraints.weightx = 1.0;// If there is extra room horizontally, the textfields will receive it
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; public class Sheet extends JFrame{ private String[] line
import java.awt.Graphics; import javax.swing.*; public class Demo { JFrame jf; JLabel[] labels; JPanel panel;
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
import javax.swing.*; import javax.swing.text.DateFormatter; import java.awt.*; import java.awt.event.*; import java.text.SimpleDateFormat; import java.util.GregorianCalendar; public class
package test; import java.awt.*; import java.awt.event.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import javax.swing.*; public class
Here is the code: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class TestGrid {
Here is the code: import javax.swing.SwingUtilities; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import java.awt.event.*;
package puzzle; import java.awt.Color; import javax.swing.*; import java.util.*; public class Puzzle { Integer x
import java.awt.*; import java.awt.image.ImageObserver; import javax.swing.*; public class CharacterMove { CharacterCollision CharacterCollision; public static
import javax.swing.SwingUtilities; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.BorderFactory; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics;

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.