public class GridBagLayout

  1. Object
  2. Layout
  3. GridBagLayout

Codename One port of the infamous GridBagLayout based on the Apache Harmony code. For new applications we generally recommend a “native” Codename One layout such as com.codename1.ui.table.TableLayout and recommend avoiding this layout. Its here for developers who are accustomed to it and need to port existing code.

Button button;
hi.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;

button = new Button("Button 1");
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
hi.addComponent(c, button);

button = new Button("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
hi.addComponent(c, button);

button = new Button("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
hi.addComponent(c, button);

button = new Button("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;      //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
hi.addComponent(c, button);

button = new Button("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0;       //reset to default
c.weighty = 1.0;   //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0);  //top padding
c.gridx = 1;       //aligned with button 2
c.gridwidth = 2;   //2 columns wide
c.gridy = 2;       //third row
hi.addComponent(c, button);

Fields

protected static final int MAXGRIDSIZE = 512
protected static final int MINSIZE = 1
protected static final int PREFERREDSIZE = 2
public double[] columnWeights
public double[] rowWeights
public int[] columnWidths
public int[] rowHeights
protected Hashtable<Component, GridBagConstraints> comptable
protected GridBagConstraints defaultConstraints
protected GridBagLayoutInfo layoutInfo

Constructors

public GridBagLayout()

Methods

public void addLayoutComponent(Object constraints, Component comp, Container c)Some layouts can optionally track the addition of elements with meta-data that allows the user to “hint” on object positioning.
public void removeLayoutComponent(Component comp)Removes the component from the layout this operation is only useful if the layout maintains references to components within it
public Object getComponentConstraint(Component comp)Returns the optional component constraint
public void setConstraints(Component comp, GridBagConstraints constraints)
public void invalidateLayout(Container target)
public Dimension getPreferredSize(Container parent)Returns the container preferred size
public void layoutContainer(Container parent)Layout the given parent container children
public int[][] getLayoutDimensions()
public double[][] getLayoutWeights()
protected void arrangeGrid(Container parent)
protected GridBagConstraints lookupConstraints(Component comp)
protected void adjustForGravity(GridBagConstraints constraints, Rectangle r)

Inherited methods

Field details

MAXGRIDSIZE

protected static final int MAXGRIDSIZE = 512

MINSIZE

protected static final int MINSIZE = 1

PREFERREDSIZE

protected static final int PREFERREDSIZE = 2

columnWeights

public double[] columnWeights

rowWeights

public double[] rowWeights

columnWidths

public int[] columnWidths

rowHeights

public int[] rowHeights

comptable

protected Hashtable<Component, GridBagConstraints> comptable

defaultConstraints

protected GridBagConstraints defaultConstraints

layoutInfo

protected GridBagLayoutInfo layoutInfo

Constructor details

GridBagLayout

public GridBagLayout()

Method details

addLayoutComponent

public void addLayoutComponent(Object constraints, Component comp, Container c)
Some layouts can optionally track the addition of elements with meta-data that allows the user to “hint” on object positioning.

Parameters

constraints Object
optional meta data information, like alignment orientation
comp Component
the added component to the layout
c Container
the parent container

removeLayoutComponent

public void removeLayoutComponent(Component comp)
Removes the component from the layout this operation is only useful if the layout maintains references to components within it

Parameters

comp Component
the removed component from layout

getComponentConstraint

public Object getComponentConstraint(Component comp)
Returns the optional component constraint

Parameters

comp Component
the component whose constraint should be returned

Returns

the optional component constraint

setConstraints

public void setConstraints(Component comp, GridBagConstraints constraints)

invalidateLayout

public void invalidateLayout(Container target)

getPreferredSize

public Dimension getPreferredSize(Container parent)
Returns the container preferred size

Parameters

parent Container
the parent container

Returns

the container preferred size

layoutContainer

public void layoutContainer(Container parent)
Layout the given parent container children

Parameters

parent Container
the given parent container

getLayoutDimensions

public int[][] getLayoutDimensions()

getLayoutWeights

public double[][] getLayoutWeights()

arrangeGrid

protected void arrangeGrid(Container parent)

lookupConstraints

protected GridBagConstraints lookupConstraints(Component comp)

adjustForGravity

protected void adjustForGravity(GridBagConstraints constraints, Rectangle r)