This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

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.
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!