File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GridBagConstraints Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GridBagConstraints" Watch "GridBagConstraints" New topic


Ranch Hand

Joined: Oct 02, 1999
Posts: 31
Which of the following is irrelevant if fill=Fill.BOTH is selected for the component?

Is it only A).

Joined: Jan 09, 2000
Posts: 24
Yes bcoz Fill and Anchor cannot go together
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Isn't the correct answer A,C and F?
Here is a link to a great GridBagLayout simulator:
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
ipadx and ipady are not irrelevant. To see this on the simulator you cite, try setting them to large values like 100.
Only answer A is correct.

"I'm not back." - Bill Harding, Twister
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I'm sorry but I'm still confused. If ipadx and ipady are NOT irrelevant than how can the answer be A only?
The GridBagLayout simulator i mentioned, how accurately does it represent the way GridBagLayout actually works? Can weightx and weighty be greater than 1? It appears that fill and anchor don't have much effect unless the weight values are greater than zero.
[This message has been edited by Joe Java (edited February 14, 2000).]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Gotta read the question - it asks which of the following ARE irrelevant, so if I say that ipadx and ipady AREN'T irrelevant, then that means they're not part of the answer. Anchor (and only anchor) IS irrelevant if fill = BOTH, so it's the only answer.
The simulator is pretty accurate, I think, since it seems to use an actual GridBagLayout to generate what you see. You do get some strange effects if weight is 0, and so it would probably be good if the simulator defaulted all weights to 1 instead. Oh well, you can do that yourself I guess. Weights certainly can be greater than 1 - they are compared to each other, so all weights are relative to whatever is the largest number used for a weight in that GridBagLayout. Often people arbitrarily limit that maximum to 1, but that's not required.
Elena Balland

Joined: Dec 21, 2000
Posts: 8
This my 2 cents.
I made the following code to test this question:
import java.awt.*;
public class BagConst1 extends java.applet.Applet {
void buildConstraints(GridBagConstraints gbc, int gx, int gy,
int gw, int gh, int wx, int wy) {
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.ipadx = wx;
gbc.ipady = wy;
public void init() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx =5;
// 1stButton
buildConstraints(constraints, 0, 0, 1, 1, 0, 0);
Button but1 = new Button("Button1");
gridbag.setConstraints(but1, constraints);

// 2nd Button
Button but2 = new Button("Button2 with ipadx");
buildConstraints(constraints, 0, 1, 1, 1, 80, 0);
gridbag.setConstraints(but2, constraints);

buildConstraints(constraints, 0, 2, 1, 1, 0, 80);

Button but3 = new Button("Button3 with ipady");
gridbag.setConstraints(but3, constraints);

buildConstraints(constraints, 0, 3, 1, 1, 0, 0);
constraints.insets = new Insets(20,60,40,40);
Button but4 = new Button("Button4 with Insets");
gridbag.setConstraints(but4, constraints);

buildConstraints(constraints, 0, 4, 1, 1, 0, 0);
constraints.insets = new Insets(0,0,0,0);
constraints.fill = GridBagConstraints.NONE;
Button but5 = new Button("Button5 without BOTH");
gridbag.setConstraints(but5, constraints);

When I compiled I received something strange:
ipadx doesn't effect, but ipady effects. Insets for sure effect.
It is pretty much fair that anchor is irrelevant for it is affects only when the component is smaller than its display area.
natarajan meghanathan
Ranch Hand

Joined: Feb 01, 2001
Posts: 130
I think the correct answer is A, C, F. Still this post is not completely answered!!

Sun Certified Programmer for Java 2 Platform
I agree. Here's the link:
subject: GridBagConstraints
jQuery in Action, 3rd edition