francesc
francesc

Reputation: 31

cannot make GridBagLayout work as desired

/**
 *  I'd like to achive following layout:
 *   +----------+----------+
 *   | Button 1 |          |
 *   +----------| Button 2 |
 *   | Button 3 |          |
 *   +----------+----------+
 *  with following code:
 */

JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton button;

button = new JButton("Button 1");
c.weightx = 0.5;
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
panel.add(button, c);

button = new JButton("Button 2");
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = 2;
panel.add(button, c);

button = new JButton("Button 3");
c.gridwidth = 1;
panel.add(button, c);

/**
 *  but what I achive is:
 *   +----------+----------+
 *   | Button 1 | Button 2 |
 *   +----------+----------+|
 *   | Button 3 |
 *   +----------+
 */

/**
 *  However layout:
 *   +----------+----------+
 *   |          | Button 2 |
 *   + Button 1 +----------+
 *   |          | Button 3 |
 *   +----------+----------+
 *  is easily achieved as:
 */

JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton button;

button = new JButton("Button 1");
c.weightx = 0.5;
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
c.gridheight = 2;
panel.add(button, c);

button = new JButton("Button 2");
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = 1;
panel.add(button, c);

button = new JButton("Button 3");
panel.add(button, c);

Any clues?

regards, Francesc

Upvotes: 1

Views: 594

Answers (7)

JiP
JiP

Reputation: 3258

Yes, the others are right. You have to manually set GridX and GridY.

Moreover, I recommend using a new GridBagConstraints instance for every component as in a more complicated forms you can easily loose track of what values are really set in the one instance you are using. In addition, you do not have to care about default values if you use the constructor with many parameters as you have to specify them all every time. It takes time to get used to it, but it pays back later on.


    /**
     *   +----------+----------+
     *   | Button 1 |          |
     *   +----------| Button 2 |
     *   | Button 3 |          |
     *   +----------+----------+
     */

    JPanel panel = new JPanel(new GridBagLayout());

    JButton b1 = new JButton("Button 1");
    panel.add(b1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
        GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0,
            0), 0, 0));

    JButton b2 = new JButton("Button 2");
    panel.add(b2, new GridBagConstraints(1, 0, 1, 2, 0.0, 0.0,
        GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0,
            0), 0, 0));

    JButton b3 = new JButton("Button 3");
    panel.add(b3, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
        GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0,
            0), 0, 0));

Upvotes: 0

Codemwnci
Codemwnci

Reputation: 54884

You have not specified the gridx and gridy settings for your elements.

JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton button;

button = new JButton("Button 1");
c.weightx = 0.5;
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
panel.add(button, c);

button = new JButton("Button 2");
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = 2;
c.gridx = 1;
c.gridy = 0;
panel.add(button, c);

button = new JButton("Button 3");
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
panel.add(button, c);

Note: the default value for gridx and gridy is 0, but it is best practice to always set the value. It is clearer, and relying on default values can cause problems when copying and pasting etc. This way is therefore clearer and safer (albeit slightly more verbose).

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

Why not instead use nested JPanels that use GridLayouts, such as:

import java.awt.GridLayout;
import javax.swing.*;

public class GridLayoutEg {
    public static void main(String[] args) {
        JPanel leftPanel = new JPanel(new GridLayout(0, 1));
        leftPanel.add(new JButton("Button 1"));
        leftPanel.add(new JButton("Button 3"));

        JPanel mainPanel = new JPanel(new GridLayout(1, 0));
        mainPanel.add(leftPanel);
        mainPanel.add(new JButton("Button 2"));

        JFrame frame = new JFrame("Foo");
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Or is there more to the story that we need to know?

Upvotes: 1

Sofahamster
Sofahamster

Reputation: 761

You have to set c.gridx for the last button. Per default, components are placed relative to each other. If you set gridx to 0 for button 3, you force it to the first column.

JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton button;

button = new JButton("Button 1");
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
panel.add(button, c);

button = new JButton("Button 2");
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = GridBagConstraints.REMAINDER;        
panel.add(button, c);

button = new JButton("Button 3");
c.gridx = 0;
c.gridwidth = 1;
c.gridheight = 1;
panel.add(button, c);

Upvotes: 1

albb
albb

Reputation: 234

You need to specify gridx and gridy, defaults are 0s.

    button = new JButton("Button 1");
    c.weightx = 0.5;
    c.weighty = 0.5;
    c.fill = GridBagConstraints.BOTH;
    panel.add(button, c);

    button = new JButton("Button 3");
    c.gridy = 1;
    c.gridwidth = 1;
    panel.add(button, c);

    button = new JButton("Button 2");
    c.gridx = 1; c.gridy = 0;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weighty = 1;c.gridheight = 2;
    panel.add(button, c);

Upvotes: 1

peter.murray.rust
peter.murray.rust

Reputation: 38043

GridBagLayout is knowln to be diffficul (see http://www.horstmann.com/articles/Taming_the_GridBagLayout.html):

Of the standard layout managers of the Java SDK, the GridBagLayout is the most useful one, but its complexity has also been known to strike fear in the hearts of programmers. Part of the complexity lies in the tedium of setting up the grid bag constraints.

I would read these articles and also ask on forums (e.g. http://www.java-forums.org/new-java/28857-gridbaglayout-problems-questions.html)

Upvotes: 0

Freddie
Freddie

Reputation: 1707

you need to set gridx and gridy on the constraints. Check out this tutorial: http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Upvotes: 0

Related Questions