I am learning GridBagLayout in swing using tutorials form Oracle.
As we all know, we can set the constraint again after being used by 1 component.Here I noticed the author is set the constraint again before call the method add().
public class GridBagLayoutDemo {
//Here I have no idea why these 3 lines for
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
//1)why not just ignore the above declaration and just type
//pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); ?
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//same go to here
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
//2)the author set the constraint in above line,then author set it again in below line
button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
//here it is
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
Anyone please answer both my questions?
I think that the second layout orientation: c.fill = GridBagConstraints.HORIZONTAL;
is a mistake, the first one is conditional and depends from the previous seted value "shouldFill". So I do not agree that this variable does nothing.