Class GridLayout2

  • All Implemented Interfaces:
    java.awt.LayoutManager, java.io.Serializable

    public class GridLayout2
    extends java.awt.GridLayout
    This enhancement enables a GridLayout to emulate a GridBagLayout. Especially this layout allows components of different sizes. This code is extracted from the JavaWorld Magazine
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      GridLayout2()
      Generates a GridLayout with 1 row (x-axis) and zero columns (y-axis).
      GridLayout2​(int rows, int cols)
      Generates a GridLayout with rows rows and cols columns.
      GridLayout2​(int rows, int cols, int hgap, int vgap)
      Generates a GridLayout with rows row, cols columns, a horizontal gap of hgap and a vertical gap of vgap The vertical gap is the distance between two objects in a column (y-axis) The horizontal gap is the distance between two objects in a row (x-axis)
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void layoutContainer​(java.awt.Container parent)
      From the web page: "Again, you first ensure you have the right number of rows and columns to lay out the components.
      java.awt.Dimension minimumLayoutSize​(java.awt.Container parent)
      From the web page: "The code for minimumLayoutSize() is basically the same as preferredLayoutSize(), except you use the subcomponents' minimum size dimensions."
      java.awt.Dimension preferredLayoutSize​(java.awt.Container parent)
      from the web page: "As you can see, the code is pretty straightforward.
      • Methods inherited from class java.awt.GridLayout

        addLayoutComponent, getColumns, getHgap, getRows, getVgap, removeLayoutComponent, setColumns, setHgap, setRows, setVgap, toString
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • GridLayout2

        public GridLayout2()
        Generates a GridLayout with 1 row (x-axis) and zero columns (y-axis).
      • GridLayout2

        public GridLayout2​(int rows,
                           int cols)
        Generates a GridLayout with rows rows and cols columns.
      • GridLayout2

        public GridLayout2​(int rows,
                           int cols,
                           int hgap,
                           int vgap)
        Generates a GridLayout with rows row, cols columns, a horizontal gap of hgap and a vertical gap of vgap The vertical gap is the distance between two objects in a column (y-axis) The horizontal gap is the distance between two objects in a row (x-axis)
    • Method Detail

      • preferredLayoutSize

        public java.awt.Dimension preferredLayoutSize​(java.awt.Container parent)
        from the web page: "As you can see, the code is pretty straightforward. You first ensure that you have the right number of rows and columns to lay out the components. Then you find each component's preferred size. Finally, you compute each row's height as the row components' maximum height. You compute each column width as the maximum width of the row components. The preferred layout size also accounts for the horizontal and vertical gap sizes between components and the parent container insets."
        Specified by:
        preferredLayoutSize in interface java.awt.LayoutManager
        Overrides:
        preferredLayoutSize in class java.awt.GridLayout
      • minimumLayoutSize

        public java.awt.Dimension minimumLayoutSize​(java.awt.Container parent)
        From the web page: "The code for minimumLayoutSize() is basically the same as preferredLayoutSize(), except you use the subcomponents' minimum size dimensions."
        Specified by:
        minimumLayoutSize in interface java.awt.LayoutManager
        Overrides:
        minimumLayoutSize in class java.awt.GridLayout
      • layoutContainer

        public void layoutContainer​(java.awt.Container parent)
        From the web page: "Again, you first ensure you have the right number of rows and columns to lay out the components. Then you compute the scaling factors on x and y coordinates as the ratios between the parent container's current height and width and the height and width of its preferred layout size. You find each component's preferred size and scale it using the scaling factors. You also compute each row's height as the maximum height of the scaled components in the row. You compute each column's width as the maximum width of the scaled components in the row. Finally, you lay out the components, considering the horizontal and vertical gap sizes between components and the parent container insets. You could also scale the horizontal and vertical gap sizes."
        Specified by:
        layoutContainer in interface java.awt.LayoutManager
        Overrides:
        layoutContainer in class java.awt.GridLayout