# Class GridBagLayout

java.lang.Object
com.codename1.ui.layouts.Layout
com.codename1.ui.layouts.GridBagLayout

public class GridBagLayout extends Layout

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);

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `double[]`
  `columnWeights`
  `int[]`
  `columnWidths`
  `protected Hashtable<Component, GridBagConstraints>`
  `comptable`
  `protected GridBagConstraints`
  `defaultConstraints`
  `protected com.codename1.ui.layouts.GridBagLayoutInfo`
  `layoutInfo`
  `protected static final int`
  `MAXGRIDSIZE`
  `protected static final int`
  `MINSIZE`
  `protected static final int`
  `PREFERREDSIZE`
  `int[]`
  `rowHeights`
  `double[]`
  `rowWeights`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `GridBagLayout()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `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.
  `protected void`
  `adjustForGravity(GridBagConstraints constraints, Rectangle r)`
  `protected void`
  `arrangeGrid(Container parent)`
  `Object`
  `getComponentConstraint(Component comp)`
  Returns the optional component constraint
  `int[][]`
  `getLayoutDimensions()`
  `double[][]`
  `getLayoutWeights()`
  `Dimension`
  `getPreferredSize(Container parent)`
  Returns the container preferred size
  `void`
  `invalidateLayout(Container target)`
  `void`
  `layoutContainer(Container parent)`
  Layout the given parent container children
  `protected GridBagConstraints`
  `lookupConstraints(Component comp)`
  `void`
  `removeLayoutComponent(Component comp)`
  Removes the component from the layout this operation is only useful if the layout maintains references to components within it
  `void`
  `setConstraints(Component comp, GridBagConstraints constraints)`

  ### Methods inherited from class Layout

  `cloneConstraint, equals, getChildrenInTraversalOrder, hashCode, isConstraintTracking, isOverlapSupported, obscuresPotential, overridesTabIndices, updateTabIndices`
  Modifier and Type
  Method
  Description
  `Object`
  `cloneConstraint(Object constraint)`
  `boolean`
  `equals(Object o)`
  Indicates whether some other object is "equal to" this one.
  `protected Component[]`
  `getChildrenInTraversalOrder(Container parent)`
  Gets the children of the parent container in the order that they should be traversed when tabbing through a form.
  `int`
  `hashCode()`
  Returns a hash code value for the object.
  `boolean`
  `isConstraintTracking()`
  If this method returns true, the addLayoutComponent method will be called when replacing a layout for every component within the container
  `boolean`
  `isOverlapSupported()`
  This method returns true if the Layout allows Components to Overlap.
  `boolean`
  `obscuresPotential(Container parent)`
  Some layout managers can obscure their child components in some cases this returns true if the basic underpinnings are in place for that.
  `boolean`
  `overridesTabIndices(Container parent)`
  If a layout specifies a different traversal order of its components than the component index, then it should override this method to return true, and it should also override `#getChildrenInTraversalOrder(com.codename1.ui.Container)` to set the tab indices of a container's children.
  `final int`
  `updateTabIndices(Container parent, int offset)`
  Updates the tab traversal order

  ### Methods inherited from class Object

  `clone, getClass, notify, notifyAll, toString, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  `final Class`
  `getClass()`
  Returns the runtime class of an object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `String`
  `toString()`
  Returns a string representation of the object.
  `final void`
  `wait()`
  Causes current thread to wait until another thread invokes the method or the method for this object.
  `final void`
  `wait(long timeout)`
  Causes current thread to wait until either another thread invokes the method or the method for this object, or a specified amount of time has elapsed.
  `final void`
  `wait(long timeout, int nanos)`
  Causes current thread to wait until another thread invokes the method or the method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

- ## Field Details

  - ### MAXGRIDSIZE

    protected static final int MAXGRIDSIZE
    See Also:
      - Constant Field Values

  - ### MINSIZE

    protected static final int MINSIZE
    See Also:
      - Constant Field Values

  - ### PREFERREDSIZE

    protected static final int PREFERREDSIZE
    See Also:
      - Constant Field Values

  - ### 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 com.codename1.ui.layouts.GridBagLayoutInfo layoutInfo

- ## Constructor Details

  - ### GridBagLayout

    public GridBagLayout()

- ## Method Details

  - ### addLayoutComponent

    public void addLayoutComponent(Object constraints, Component comp, Container c)
    Description copied from class: `Layout`

    Some layouts can optionally track the addition of elements with meta-data that allows the user to "hint" on object positioning.

    ###### Parameters

    - `value`: optional meta data information, like alignment orientation

    - `comp`: the added component to the layout

    - `c`: the parent container

    Overrides:
      `addLayoutComponent` in class `Layout`

  - ### removeLayoutComponent

    public void removeLayoutComponent(Component comp)
    Description copied from class: `Layout`

    Removes the component from the layout this operation is only useful if the layout maintains references to components within it

    ###### Parameters

    - `comp`: the removed component from layout
    Overrides:
      `removeLayoutComponent` in class `Layout`

  - ### getComponentConstraint

    public Object getComponentConstraint(Component comp)
    Description copied from class: `Layout`

    Returns the optional component constraint

    ###### Parameters

    - `comp`: the component whose constraint should be returned

    ###### Returns

    the optional component constraint

    Overrides:
      `getComponentConstraint` in class `Layout`

  - ### setConstraints

    public void setConstraints(Component comp, GridBagConstraints constraints)

  - ### invalidateLayout

    public void invalidateLayout(Container target)

  - ### getPreferredSize

    public Dimension getPreferredSize(Container parent)
    Description copied from class: `Layout`

    Returns the container preferred size

    ###### Parameters

    - `parent`: the parent container

    ###### Returns

    the container preferred size

    Specified by:
      `getPreferredSize` in class `Layout`

  - ### layoutContainer

    public void layoutContainer(Container parent)
    Description copied from class: `Layout`

    Layout the given parent container children

    ###### Parameters

    - `parent`: the given parent container
    Specified by:
      `layoutContainer` in class `Layout`

  - ### 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)