This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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
Author

GridBagConstraints

Siddhu
Ranch Hand

Joined: Oct 02, 1999
Posts: 31
Which of the following is irrelevant if fill=Fill.BOTH is selected for the component?
A)anchor
B)insets
C)ipadx
D)gridx
E)gridwidth
F)ipady

Is it only A).
Venkat01
Greenhorn

Joined: Jan 09, 2000
Posts: 24
Yes bcoz Fill and Anchor cannot go together
Anonymous
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:
http://www.austria.eu.net/java/programs/applets/missgable/index.htm
Jim Yingst
Wanderer
Sheriff

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
Anonymous
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
Wanderer
Sheriff

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
Greenhorn

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();
setLayout(gridbag);
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);
add(but1);

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

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

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

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);
add(but4);

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);
add(but5);

}
}
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
 
GeeCON Prague 2014
 
subject: GridBagConstraints