java GridBagLayout anchor
Asked Answered
C

1

7

Learing GridBagLayout, The issue here is, the name label and combox don't show up on the top of the panel, but I have set its anchor to NORTH. Why ?

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class Test2 {    
    public Test2() {
        JFrame frame = new JFrame();
        frame.setTitle("test");
        frame.getContentPane().setLayout(new GridLayout(1,2));
        frame.setSize(800, 600);

        JPanel panel1 = new JPanel();
        panel1.setLayout(new GridBagLayout());

        JLabel label = new JLabel("name");
        GridBagConstraints gridBagConstraints = new GridBagConstraints();   
        gridBagConstraints.anchor = GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 0.0;
        gridBagConstraints.weighty = 0.0;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        panel1.add(label, gridBagConstraints);

        String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
        JComboBox petList = new JComboBox(petStrings);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.anchor = GridBagConstraints.NORTH;
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.0;
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        panel1.add(petList, gridBagConstraints);    

        frame.getContentPane().add(panel1);
        frame.getContentPane().add(new JPanel());       

        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);      
    }

    public static void main(String[] args) {
        new Test2();
    }
}
Caucasia answered 29/1, 2011 at 23:10 Comment(0)
O
19

You have to change

gridBagConstraints.weighty = 0.0;

to

gridBagConstraints.weighty = 1.0;

otherwise the area reserved for the component is slimmed to the size of the component, and it doesn't matter in which direction you "anchor" the component.

The result after changing the weighty is the following:

enter image description here

Oliverolivera answered 29/1, 2011 at 23:14 Comment(6)
yes,you are right. do you mean, when anchor set to NORTH, then weighty has to be 1.0 ?Caucasia
Well, if you want to the component to be given some "area" (larger than the component itself) to be placed in, you need a non-zero weight. (I.e., weight 0.1 would work too in this particular case.)Oliverolivera
I always confuse the double value, what is the difference between 1.0 and 0.5 ?Caucasia
If you have another component above or below, the weights determine which component gets the most space. If you have one with weight 1.0 and another with weight 3.0, then they get 25% and 75% of the available space respectively.Oliverolivera
In my memory, the weightx and weighty, the maximum value is 1.0, is it correct ?Caucasia
No, I don't believe they have an upper limit.Oliverolivera

© 2022 - 2024 — McMap. All rights reserved.