Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GridBagLayout: Does not appeared properly.

 
kishor patankar
Greenhorn
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.
thanks,
Kishor
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)
{
dispose();
System.gc();
System.exit(0);
}
});
/* Size the frame */
setSize(800,600);

/* Main window divided into two panes
mainPanel.setLayout(new GridLayout(2,1));
/* Construct the Upper panel/
Panel upperPanel = new Panel();
upperPanel.setLayout(theBag);
/* Adding table */
add(BorderLayout.EAST,lowerFrame);
lowerFrame.add(lowerPanel);
lowerFrame.setSize(800,300);
lowerPanel.setLayout(theBag);
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 */
setVisible(true);
/* 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);
thePanel.add(theComponent);
}
}
 
Matthew Jones
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
Greenhorn
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic