Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GridBagConstraints

 
Siddhu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes bcoz Fill and Anchor cannot go together
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the correct answer is A, C, F. Still this post is not completely answered!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic