Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes how to use GridBagLayout? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to use GridBagLayout?" Watch "how to use GridBagLayout?" New topic

how to use GridBagLayout?

Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
I want to use GridBagLayout to mangae my swing interface display
2 columns and rows:
For example:
First Name: txtField
Last Name: txtField2
I know i can use gridX, and gridY to specify the cell location, However it would be easier to GridBagConstraints.REMAINDER and GridBagConstraints.RELATIVE when you have more rows and columns. I don't understand how to use it, Can anyone show me how?

Tom Pelly

Joined: Jun 29, 2001
Posts: 14
I looks to me like it would be simpler just to use a GridLayout(2,2) for what you want.
GridBayLayout can seem quite compilcated at first, but it not too bad once you get used to it.
I think you'd need something like this:
myPanel.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1;
constraints.weighty = 1;
constraints.gridx = 0;
constraints.gridy = 0;
myPanel.add(PanelOne, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
myPanel.add(PanelTwo, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
myPanel.add(PanelThree, constraints);
constraints.gridx = 1;
constraints.gridy = 1;
myPanel.add(PanelFour, constraints);

Sorry if there are any errors in there, but I think it should basicly work!

I agree. Here's the link:
subject: how to use GridBagLayout?