Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

GridBagLayout: Does not appeared properly.

Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friend,
I am trying to use the GridBagLayout for my table purpose. The column width will be depend on the size of the field.
But, it is not showing me the layout correcly. I don't know what is going wrong. Or the layout itself work this way? Pl. help me to come out from this situation. for your reference i am attaching the code.
import java.awt.*;
import java.awt.event.*;
public class Rcpt1 extends Frame
Panel lowerPanel = new Panel();
ScrollPane lowerFrame = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
GridBagLayout theBag = new GridBagLayout();
GridBagConstraints theGBC = new GridBagConstraints();
TextField[] theScroll1 = new TextField[999];
TextField[] theScroll2 = new TextField[999];
TextField[] theScroll3 = new TextField[999];
TextField[] theButton1 = new TextField[999];
public static void main (String args[])
new Rcpt1();
public Rcpt1()
super("Goods Receipt");
/* Connect to the database */
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent evt)
/* Size the frame */

/* Main window divided into two panes
mainPanel.setLayout(new GridLayout(2,1));
/* Construct the Upper panel/
Panel upperPanel = new Panel();
/* Adding table */
theGBC.fill = GridBagConstraints.HORIZONTAL;
theGBC.insets = new Insets(0,0,0,0);
theGBC.weightx = 1.0;
theGBC.weighty = 1.0;
theGBC.anchor = GridBagConstraints.NORTH;
for (int i = 0; i < 5; i++)
addComponent(i, 0, 1, 1, theButton1 [i] = new TextField(""+(i+1)), lowerPanel);
addComponent(i, 1, 1, 1, theScroll1 [i] = new TextField(20), lowerPanel);
addComponent(i, 2, 1, 1, theScroll2 [i] = new TextField(10), lowerPanel);
addComponent(i, 3, 1, 1, theScroll3 [i] = new TextField(40), lowerPanel);
theButton1 [i].setEnabled(false);
/* Display the frame */
/* Center the frame */
Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle frameDim = getBounds();
setLocation((screenDim.width - frameDim.width) / 2,(screenDim.height - frameDim.height) / 2);
public void addComponent(int row, int col, int high, int wide, Component theComponent, Panel thePanel)
theGBC.gridx = col;
theGBC.gridy = row;
theGBC.gridwidth = wide;
theGBC.gridheight = high;
theBag.setConstraints(theComponent, theGBC);
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said that the column width will depend on the size of the field, but when you set the constraints, you set the width to 1 for all of the columns.
If you want the width different, you have to change the width when you are setting the constraints. The width is a proportion, so if you set column 1 to width 2, and column 2 to width 1, column 1 would be twice as wide as column 2
kishor patankar
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the advice.
there is another problem. its show me the gap within two rows. Since, i mentioned the north all the row should be one after another, provided i set the insets. why it is so?
pl. help.
I think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic