Nikos
Nikos

Reputation: 3

Some Problems With GridBagLayout

I'm trying to understand the basic concepts of Swing and currently I'm taking a look at GridBagLayout. Anyway I'm trying to produce the layout in the picture using only GridBagLayout: https://i.sstatic.net/pRx82.jpg

I managed to produce it embedding a panel gridBagHelper with gridbag layout inside the main frame which also has gridbaglayout.

Here are the two classes that produce the desired layout:

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComponent;
import javax.swing.JFrame;
import Borders.BorderPanel;

public class GridBagFrame extends JFrame
{
private static final long serialVersionUID = 1L;
private GridBagLayout gbl = new GridBagLayout();
private GridBagHelper gridBagHelper;


public GridBagFrame()
{
    this.setSize(200,200);
    this.setLocation(200,200);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(new Dimension(600,600));
    this.getContentPane().setLayout(gbl);

    gridBagHelper = new GridBagHelper();
    BorderPanel panelA = new BorderPanel("A");
    BorderPanel panelB = new BorderPanel("B");
    BorderPanel panelD = new BorderPanel("D");
    BorderPanel panelF = new BorderPanel("F");
    BorderPanel panelG = new BorderPanel("G");
    BorderPanel panelH = new BorderPanel("H");
    BorderPanel panelJ = new BorderPanel("J");

    this.getContentPane().add(panelA);
    this.getContentPane().add(panelB);
    this.getContentPane().add(gridBagHelper);
    this.getContentPane().add(panelD);
    this.getContentPane().add(panelF);
    this.getContentPane().add(panelG);
    this.getContentPane().add(panelH);
    this.getContentPane().add(panelJ);

    easyConstraints(gbl, panelA, 1, 3, 0, 0, 1.0, 1.0);
    easyConstraints(gbl, panelB, 1, 2, 1, 0, 3.0, 2.0);
    easyConstraints(gbl, gridBagHelper, 1, 3, 2, 0, 1.0, 1.0);
    easyConstraints(gbl, panelD, 1, 2, 3, 0, 1.0, 2.0);
    easyConstraints(gbl, panelF, 1, 1, 1, 2, 3.0, 2.0);
    easyConstraints(gbl, panelG, 1, 1, 3, 2, 1.0, 2.0);
    easyConstraints(gbl, panelH, 2, 1, 0, 3, 1.0, 1.0);
    easyConstraints(gbl, panelJ, 2, 1, 2, 3, 1.0, 1.0);

}

private void easyConstraints(GridBagLayout GLB, JComponent comp, int w,
        int h, int x, int y, double wx, double wy)
{
    GridBagConstraints constraints= new GridBagConstraints();
    constraints.fill=GridBagConstraints.BOTH;
    constraints.gridwidth = w;
    constraints.gridheight = h;
    constraints.gridx = x;
    constraints.gridy = y;
    constraints.weightx = wx;
    constraints.weighty = wy;
    GLB.setConstraints(comp, constraints);
}


public static void main (String[] args)
{
    GridBagFrame gridBagFrame = new GridBagFrame();
    gridBagFrame.setVisible(true);
}
}

and the helper class for panels C and E:

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComponent;
import javax.swing.JPanel;

import Borders.BorderPanel;

public class GridBagHelper extends JPanel
{
private static final long serialVersionUID = 1L;
private GridBagLayout gbl = new GridBagLayout();

public GridBagHelper()
{
    this.setSize(200,200);
    this.setLocation(200,200);
    this.setSize(new Dimension(600,600));
    this.setLayout(gbl);
    BorderPanel panelC = new BorderPanel("C");
    BorderPanel panelE = new BorderPanel("E");

    this.add(panelC);
    this.add(panelE);

    easyConstraints(gbl, panelC, 1, 1, 0, 0, 1.0, 1.0);
    easyConstraints(gbl, panelE, 1, 3, 0, 1, 1.0, 3.0);
}

private void easyConstraints(GridBagLayout GLB, JComponent comp, int w,
        int h, int x, int y, double wx, double wy)
{
    GridBagConstraints constraints= new GridBagConstraints();
    constraints.fill=GridBagConstraints.BOTH;
    constraints.gridwidth = w;
    constraints.gridheight = h;
    constraints.gridx = x;
    constraints.gridy = y;
    constraints.weightx = wx;
    constraints.weighty = wy;
    GLB.setConstraints(comp, constraints);
}

}

in the meantime I spend many hours trying to make it work using only one gridbaglayout with no luck. There was no way to convince borderpanelC not to expand and also no way to convince borderpanelE to start from the middle of B. I would like to know if anyone knows the way to make it work in just one Frame with just one gridbaglayout and what are the right values for widths, heights and weights for this. Thank you very much in advance!

Edit: Ok, In case it helps to help me here is what I tried

  import java.awt.Dimension;
  import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;

  import javax.swing.JComponent;
  import javax.swing.JFrame;
  import Borders.BorderPanel;

  public class GridBagFrameBad extends JFrame
{
private static final long serialVersionUID = 1L;
private GridBagLayout gbl = new GridBagLayout();

public GridBagFrameBad()
{
    this.setTitle("This is not supposed to work");
    this.setSize(200,200);
    this.setLocation(200,200);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(new Dimension(600,600));
    this.getContentPane().setLayout(gbl);


    BorderPanel panelA = new BorderPanel("A");
    BorderPanel panelB = new BorderPanel("B");
    BorderPanel panelC = new BorderPanel("C");
    BorderPanel panelD = new BorderPanel("D");
    BorderPanel panelE = new BorderPanel("E");
    BorderPanel panelF = new BorderPanel("F");
    BorderPanel panelG = new BorderPanel("G");
    BorderPanel panelH = new BorderPanel("H");
    BorderPanel panelJ = new BorderPanel("J");

    this.getContentPane().add(panelA);
    this.getContentPane().add(panelB);
    this.getContentPane().add(panelC);
    this.getContentPane().add(panelD);
    this.getContentPane().add(panelE);
    this.getContentPane().add(panelF);
    this.getContentPane().add(panelG);
    this.getContentPane().add(panelH);
    this.getContentPane().add(panelJ);

    easyConstraints(gbl, panelA, 1, 3, 0, 0, 1.0, 1.0);
    easyConstraints(gbl, panelB, 1, 2, 1, 0, 3.0, 2.0);
    easyConstraints(gbl, panelC, 1, 1, 2, 0, 1.0, 1.0);
    easyConstraints(gbl, panelD, 1, 2, 3, 0, 1.0, 2.0);
    easyConstraints(gbl, panelE, 1, 2, 2, 1, 1.0, 1.0);
    easyConstraints(gbl, panelF, 1, 1, 1, 2, 3.0, 2.0);
    easyConstraints(gbl, panelG, 1, 1, 3, 2, 1.0, 2.0);
    easyConstraints(gbl, panelH, 2, 1, 0, 3, 1.0, 1.0);
    easyConstraints(gbl, panelJ, 2, 1, 2, 3, 1.0, 1.0);

}

private void easyConstraints(GridBagLayout GLB, JComponent comp, int w,
        int h, int x, int y, double wx, double wy)
{
    GridBagConstraints constraints= new GridBagConstraints();
    constraints.fill=GridBagConstraints.BOTH;
    constraints.gridwidth = w;
    constraints.gridheight = h;
    constraints.gridx = x;
    constraints.gridy = y;
    constraints.weightx = wx;
    constraints.weighty = wy;
    GLB.setConstraints(comp, constraints);
}


public static void main (String[] args)
{
    GridBagFrameBad gridBagFrameBad = new GridBagFrameBad();
    gridBagFrameBad.setVisible(true);
}
}

Upvotes: 0

Views: 141

Answers (1)

Betlista
Betlista

Reputation: 10547

I think, you missed one important thing, that weight is from [0, 1] interval, normalize your weights, maybe it helps, let me check...

On the other hand I see, that your gridwidth and gridheight is 0, probably you do not want this also...


Ok, it seems, we have to start from the beginning...

Your code produced this for me:

enter image description here

My Java version is jdk1.7.0_67...

I used this as BorderPanel:

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JPanel;


public class BorderPanel extends JPanel {

    public BorderPanel(String title) {
        setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), title));
    }

}

Upvotes: 2

Related Questions