Class GridLayout

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

public class GridLayout extends Layout

The components are arranged in a grid based on available space, all cells in the grid are given exactly the same size which matches the largest preferred size or available space. The main use case for this layout is a grid of icons e.g. like one would see in the iPhone home screen.

If the number of rows * columns is smaller than the number of components added a new row is implicitly added to the grid. However, if the number of components is smaller than available cells (won't fill the last row) blank spaces will be left in place.

In this example we can see that a 2x2 grid is used to add 5 elements, this results in an additional row that's implicitly added turning the grid to a 3x2 grid implicitly and leaving one blank cell.

Form hi = new Form("Grid Layout 2x2", new GridLayout(2, 2));
hi.add(new Label("First")).
    add(new Label("Second")).
    add(new Label("Third")).
    add(new Label("Fourth")).
    add(new Label("Fifth"));

When we use a 2x4 size ratio we would see elements getting cropped as we do here. The grid layout uses the grid size first and doesn't pay too much attention to the preferred size of the components it holds.

Grid also has an autoFit attribute that can be used to automatically calculate the column count based on available space and preferred width. This is really useful for working with UI's where the device orientation might change.

There is also a terse syntax for working with a grid that has two versions, one that uses the "auto fit" option and another that accepts the column names. Heres a sample of the terse syntax coupled with the auto fit screenshots of the same code in two orientations:

GridLayout.encloseIn(new Label("First"),
    new Label("Second"),
    new Label("Third"),
    new Label("Fourth"),
    new Label("Fifth"));
  • Constructor Details

    • GridLayout

      public GridLayout(int rows, int columns)

      Creates a new instance of GridLayout with the given rows and columns

      Parameters
      • rows: - number of rows.

      • columns: - number of columns.

      Throws
      • IllegalArgumentException: if rows < 1 or columns < 1
    • GridLayout

      public GridLayout(int rows, int columns, int landscapeRows, int landscapeColumns)

      Creates a new instance of GridLayout with the given rows and columns

      Parameters
      • rows: - number of rows.

      • columns: - number of columns.

      • landscapeRows: - number of rows when in landscape mode

      • landscapeColumns: - number of columns when in landscape mode

      Throws
      • IllegalArgumentException: if rows < 1 or columns < 1
    • GridLayout

      public GridLayout(int columns)

      Creates a new instance of GridLayout with the given columns, rows is set to 1 but will implicitly grow if more components are added

      Parameters
      • columns: - number of columns.
      Throws
      • IllegalArgumentException: if rows < 1 or columns < 1
  • Method Details

    • autoFit

      public static GridLayout autoFit()

      Returns a grid layout that implicitly auto-fits to width in term of number of columns

      Returns

      a grid layout that automatically adapts its size

    • encloseIn

      public static Container encloseIn(Component... cmp)

      Creates a new container with an auto fit grid layout and the components added to it

      Parameters
      • cmp: the components
      Returns

      a new container

    • encloseIn

      public static Container encloseIn(int columns, Component... cmp)

      Creates a new container with the grid layout and the components added to it

      Parameters
      • columns: the number of columns for the grid

      • cmp: the components

      Returns

      a new container

    • layoutContainer

      public void layoutContainer(Container parent)

      Layout the given parent container children

      Parameters
      • parent: the given parent container
      Specified by:
      layoutContainer in class Layout
    • getPreferredSize

      public Dimension getPreferredSize(Container parent)

      Returns the container preferred size

      Parameters
      • parent: the parent container
      Returns

      the container preferred size

      Specified by:
      getPreferredSize in class Layout
    • toString

      public String toString()
      Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
      Overrides:
      toString in class Object
    • getRows

      public int getRows()
      Returns

      the rows

    • getColumns

      public int getColumns()
      Returns

      the columns

    • equals

      public boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The equals method implements an equivalence relation: It is reflexive: for any reference value x, x.equals(x) should return true. It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true. It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified. For any non-null reference value x, x.equals(null) should return false. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
      Overrides:
      equals in class Layout
    • hashCode

      public int hashCode()
      Description copied from class: Layout
      Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable. The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
      Overrides:
      hashCode in class Layout
    • isFillLastRow

      public boolean isFillLastRow()

      When set to true makes the grid layout fill the last row of the layout entirely if the number of elements in that row is bigger.

      Returns

      the fillLastRow

    • setFillLastRow

      public void setFillLastRow(boolean fillLastRow)

      When set to true makes the grid layout fill the last row of the layout entirely if the number of elements in that row is bigger.

      Parameters
      • fillLastRow: the fillLastRow to set
    • isAutoFit

      public boolean isAutoFit()

      Auto fits columns/rows to available screen space

      Returns

      the autoFit

    • setAutoFit

      public void setAutoFit(boolean autoFit)

      Auto fits columns/rows to available screen space

      Parameters
      • autoFit: the autoFit to set
    • obscuresPotential

      public 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. This method doesn't take padding/margin etc. into account since that is checked by the caller

      Parameters
      • parent: parent container
      Returns

      true if there is a chance that this layout manager can fully obscure the background, when in doubt return false...

      Overrides:
      obscuresPotential in class Layout
    • isHideZeroSized

      public boolean isHideZeroSized()

      When set to true components that have 0 size will be hidden and won't occupy a cell within the grid. This makes animating a grid layout component MUCH easier.

      Returns

      the hideZeroSized

    • setHideZeroSized

      public void setHideZeroSized(boolean hideZeroSized)

      When set to true components that have 0 size will be hidden and won't occupy a cell within the grid. This makes animating a grid layout component MUCH easier.

      Parameters
      • hideZeroSized: the hideZeroSized to set