Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does GridBagLayout center my components instead of putting it in the corner?

So far I managed to avoid using the GridBagLayout (by hand code) as much as possible, but I could not avoid it this time and I am reading the SUN's tutorial GridBagLayout So far it is not going well. I think I am missunderstanding something.
For example I try the following code (similar to the one in SUN's post):

public class MainFrame extends JFrame { 


    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainFrame frame = new MainFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame
     */
    public MainFrame() {
        super();
        setBounds(100, 100, 500, 375);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container mainContainer = getContentPane();

        mainContainer.setLayout(new GridBagLayout());       

        //add label
        JLabel someLabel = new JLabel("Label 1:");
        GridBagConstraints constraints = new GridBagConstraints();

        constraints.gridx = 0;
        constraints.gridy = 0;
        //constraints.anchor = GridBagConstraints.FIRST_LINE_START;
        //constraints.weightx = 0.5;
        mainContainer.add(someLabel, constraints);      

        JTextField someText = new JTextField(30);

        constraints = new GridBagConstraints();

        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.weightx = 0.5;
        mainContainer.add(someText, constraints);

        //
    }

}

I get the label and the textfield one next to the other in the center of the frame.
But I was expecting that they would show up in the top left corner since the gridx and gridy is 0 for the label.
Even if I set constraints.anchor = GridBagConstraints.FIRST_LINE_START; still the same result.
Am I wrong here?
From the SUN's post:

Specify the row and column at the upper left of the component. The leftmost column has address gridx=0 and the top row has address gridy=0.

like image 594
Cratylus Avatar asked Sep 01 '11 19:09

Cratylus


People also ask

What is the difference between GridLayout and GridBagLayout?

A GridLayout puts all the components in a rectangular grid and is divided into equal-sized rectangles and each component is placed inside a rectangle whereas GridBagLayout is a flexible layout manager that aligns the components vertically and horizontally without requiring that the components be of the same size.

Why do we need layout management what is GridBagLayout?

GridBagLayout. GridBagLayout is a sophisticated, flexible layout manager. It aligns components by placing them within a grid of cells, allowing components to span more than one cell. The rows in the grid can have different heights, and grid columns can have different widths.

What is Gridbag layout?

GridBagLayout is one of the most flexible — and complex — layout managers the Java platform provides. A GridBagLayout places components in a grid of rows and columns, allowing specified components to span multiple rows or columns. Not all rows necessarily have the same height.

What is weight in GridBagLayout?

GridBagLayout calculates the weight of a column to be the maximum weightx of all the components in a column. If the resulting layout is smaller horizontally than the area it needs to fill, the extra space is distributed to each column in proportion to its weight. A column with a 0.0 weight receives no extra space.


2 Answers

Add constraints.weighty = 1; to the JLabel constraints and constraints.anchor = GridBagConstraints.NORTHWEST; to the TextField constraints.

EDIT:

From Oracle's GridBagLayout guide:

Larger numbers indicate that the component's row or column should get more space. For each column, the weight is related to the highest weightx specified for a component within that column, with each multicolumn component's weight being split somehow between the columns the component is in. Similarly, each row's weight is related to the highest weighty specified for a component within that row. Extra space tends to go toward the rightmost column and bottom row.

like image 103
BenCole Avatar answered Oct 05 '22 11:10

BenCole


You need to read further down in the Swing tutorial for the section on weightX/weightY where it states:

Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container.

You specified a weightX but not a weightY.

Edit, it's more complicated than I thought. It appears you also need to specify:

constraints.anchor = GridBagConstraints.FIRST_LINE_START;

for both components in addiation to the weighty.

like image 23
camickr Avatar answered Oct 05 '22 12:10

camickr