You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

layout-gridlayout.asciidoc 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ---
  2. title: GridLayout
  3. order: 4
  4. layout: page
  5. ---
  6. [[layout.gridlayout]]
  7. = [classname]#GridLayout#
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/layout/grid-layout"]
  11. endif::web[]
  12. [classname]#GridLayout# container lays components out on a grid, defined by the
  13. number of columns and rows. The columns and rows of the grid serve as
  14. coordinates that are used for laying out components on the grid. Each component
  15. can use multiple cells from the grid, defined as an area (x1,y1,x2,y2), although
  16. they typically take up only a single grid cell.
  17. The grid layout maintains a cursor for adding components in left-to-right,
  18. top-to-bottom order. If the cursor goes past the bottom-right corner, it will
  19. automatically extend the grid downwards by adding a new row.
  20. The following example demonstrates the use of [classname]#GridLayout#. The
  21. [methodname]#addComponent# takes a component and optional coordinates. The
  22. coordinates can be given for a single cell or for an area in x,y (column,row)
  23. order. The coordinate values have a base value of 0. If coordinates are not
  24. given, the cursor will be used.
  25. [source, java]
  26. ----
  27. // Create a 4 by 4 grid layout.
  28. GridLayout grid = new GridLayout(4, 4);
  29. grid.addStyleName("example-gridlayout");
  30. // Fill out the first row using the cursor.
  31. grid.addComponent(new Button("R/C 1"));
  32. for (int i = 0; i < 3; i++) {
  33. grid.addComponent(new Button("Col " + (grid.getCursorX() + 1)));
  34. }
  35. // Fill out the first column using coordinates.
  36. for (int i = 1; i < 4; i++) {
  37. grid.addComponent(new Button("Row " + i), 0, i);
  38. }
  39. // Add some components of various shapes.
  40. grid.addComponent(new Button("3x1 button"), 1, 1, 3, 1);
  41. grid.addComponent(new Label("1x2 cell"), 1, 2, 1, 3);
  42. InlineDateField date = new InlineDateField("A 2x2 date field");
  43. date.setResolution(DateField.RESOLUTION_DAY);
  44. grid.addComponent(date, 2, 2, 3, 3);
  45. ----
  46. The resulting layout is shown in <<figure.layout.gridlayout>>.
  47. The borders have been made visible to illustrate the layout cells.
  48. [[figure.layout.gridlayout]]
  49. .The [classname]#GridLayout# component
  50. image::img/gridlayout.png[width=50%, scaledwidth=75%]
  51. A component to be placed on the grid must not overlap with existing components.
  52. A conflict causes throwing a [classname]#GridLayout.OverlapsException#.
  53. == Sizing Grid Cells
  54. You can define the size of both a grid layout and its components in either fixed
  55. or percentual units, or leave the size undefined altogether, as described in
  56. <<dummy/../../../framework/components/components-features#components.features.sizeable,"Sizing
  57. Components">>.
  58. <<dummy/../../../framework/layout/layout-settings#layout.settings.size,"Layout
  59. Size">> gives an introduction to sizing of layouts.
  60. The size of the [classname]#GridLayout# component is undefined by default, so it
  61. will shrink to fit the size of the components placed inside it. In most cases,
  62. especially if you set a defined size for the layout but do not set the contained
  63. components to full size, there will be some unused space. The position of the
  64. non-full components within the grid cells will be determined by their
  65. __alignment__. See
  66. <<dummy/../../../framework/layout/layout-settings#layout.settings.alignment,"Layout
  67. Cell Alignment">> for details on how to align the components inside the cells.
  68. The components contained within a [classname]#GridLayout# layout can be laid out
  69. in a number of different ways depending on how you specify their height or
  70. width. The layout options are similar to [classname]#HorizontalLayout# and
  71. [classname]#VerticalLayout#, as described in
  72. <<dummy/../../../framework/layout/layout-orderedlayout#layout.orderedlayout,"VerticalLayout
  73. and HorizontalLayout">>.
  74. [WARNING]
  75. .A layout that contains components with percentual size must have a defined size!
  76. ====
  77. If a layout has undefined size and a contained component has, say, 100% size,
  78. the component would fill the space given by the layout, while the layout would
  79. shrink to fit the space taken by the component, which is a paradox. This
  80. requirement holds for height and width separately. The debug mode allows
  81. detecting such invalid cases; see
  82. <<dummy/../../../framework/advanced/advanced-debug#advanced.debug.mode,"Enabling
  83. the Debug Mode">>.
  84. ====
  85. Often, you want to have one or more rows or columns that take all the available
  86. space left over from non-expanding rows or columns. You need to set the rows or
  87. columns as __expanding__ with [methodname]#setRowExpandRatio()# and
  88. [methodname]#setColumnExpandRatio()#. The first parameter for these methods is
  89. the index of the row or column to set as expanding. The second parameter for the
  90. methods is an expansion ratio, which is relevant if there are more than one
  91. expanding row or column, but its value is irrelevant if there is only one. With
  92. multiple expanding rows or columns, the ratio parameter sets the relative
  93. portion how much a specific row/column will take in relation with the other
  94. expanding rows/columns.
  95. [source, java]
  96. ----
  97. GridLayout grid = new GridLayout(3,2);
  98. // Layout containing relatively sized components must have
  99. // a defined size, here is fixed size.
  100. grid.setWidth("600px");
  101. grid.setHeight("200px");
  102. // Add some content
  103. String labels [] = {
  104. "Shrinking column<br/>Shrinking row",
  105. "Expanding column (1:)<br/>Shrinking row",
  106. "Expanding column (5:)<br/>Shrinking row",
  107. "Shrinking column<br/>Expanding row",
  108. "Expanding column (1:)<br/>Expanding row",
  109. "Expanding column (5:)<br/>Expanding row"
  110. };
  111. for (int i=0; i<labels.length; i++) {
  112. Label label = new Label(labels[i], ContentMode.HTML);
  113. label.setWidth(null); // Set width as undefined
  114. grid.addComponent(label);
  115. }
  116. // Set different expansion ratios for the two columns
  117. grid.setColumnExpandRatio(1, 1);
  118. grid.setColumnExpandRatio(2, 5);
  119. // Set the bottom row to expand
  120. grid.setRowExpandRatio(1, 1);
  121. // Align and size the labels.
  122. for (int col=0; col<grid.getColumns(); col++) {
  123. for (int row=0; row<grid.getRows(); row++) {
  124. Component c = grid.getComponent(col, row);
  125. grid.setComponentAlignment(c, Alignment.TOP_CENTER);
  126. // Make the labels high to illustrate the empty
  127. // horizontal space.
  128. if (col != 0 || row != 0)
  129. c.setHeight("100%");
  130. }
  131. }
  132. ----
  133. [[figure.ui.gridlayout.sizing.expanding]]
  134. .Expanding rows and columns in [classname]#GridLayout#
  135. image::img/gridlayout_sizing_expanding.png[width=75%, scaledwidth=100%]
  136. If the size of the contained components is undefined or fixed, the expansion
  137. ratio is of the __excess__ space, as in
  138. <<figure.ui.gridlayout.sizing.expanding>> (excess horizontal space is shown in
  139. white). However, if the size of the all the contained components in the
  140. expanding rows or columns is defined as a percentage, the ratio is calculated
  141. from the __overall__ space available for the percentually sized components. For
  142. example, if we had a 100 pixels wide grid layout with two columns with 1.0 and
  143. 4.0 respective expansion ratios, and all the components in the grid were set as
  144. [methodname]#setWidth("100%")#, the columns would have respective widths of 20
  145. and 80 pixels, regardless of the minimum size of their contained components.
  146. [[layout.gridlayout.css]]
  147. == CSS Style Rules
  148. [source, css]
  149. ----
  150. .v-gridlayout {}
  151. .v-gridlayout-margin {}
  152. ----
  153. The `v-gridlayout` is the root element of the [classname]#GridLayout# component.
  154. The `v-gridlayout-margin` is a simple element inside it that allows setting a padding between the outer element and the cells.
  155. For styling the individual grid cells, you should style the components inserted in the cells.
  156. Normally, if you want to have, for example, a different color for a certain cell, just make set the component inside it [methodname]#setSizeFull()#, and add a style name for it.
  157. Sometimes, you may need to wrap a component inside a layout component just for styling the cell.