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.

ConfiguringGridColumnWidths.asciidoc 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ---
  2. title: Configuring Grid Column Widths
  3. order: 25
  4. layout: page
  5. ---
  6. [[configuring-grid-column-widths]]
  7. = Configuring Grid column widths
  8. To try out how the widths of Grid columns work in different situations,
  9. we'll use the same base implementation as in the
  10. <<UsingGridWithAContainer#using-grid-with-a-container,
  11. Using Grid with a Container>> example.
  12. Grid does by default check the widths of all cells on the first pageful
  13. of data and allocate column widths based on that. If there's room to
  14. spare, each column gets and equal share of the extra pixels.
  15. There is usually one or maybe two columns that would most benefit from
  16. some additional breathing room, but Grid can't know which columns that
  17. is unless you tell it. You can do so using the `setExpandRatio(int)`
  18. method for a column.
  19. [source,java]
  20. ....
  21. grid.getColumn("name").setExpandRatio(1);
  22. ....
  23. When setting one column to expand, all the extra space gets allocated to
  24. that column. This might instead cause the other columns to be too
  25. tightly spaced. One easy way of avoiding this is to use `setWidth(double)`
  26. to set a pixel size for columns that are not expanded.
  27. [source,java]
  28. ....
  29. grid.getColumn("name").setExpandRatio(1);
  30. grid.getColumn("amount").setWidth(100);
  31. grid.getColumn("count").setWidth(100);
  32. ....
  33. Reducing the width of Grid does now cause the `Name` column to shrink
  34. while the two other columns keep their defined original sizes. We might,
  35. however, want to prevent the `Name` column from becoming too narrow by
  36. giving it a minimum width. Without any defined minimum width, the widths
  37. of the cell contents of the first pageful of data will define the
  38. minimum width. If there's not enough room for all columns, Grid will
  39. automatically enable horizontal scrolling so that all columns can still
  40. be accessed.
  41. [source,java]
  42. ....
  43. grid.setWidth("400px");
  44. grid.getColumn("name").setMinimumWidth(250);
  45. grid.getColumn("amount").setWidth(100);
  46. grid.getColumn("count").setWidth(100);
  47. ....
  48. With horizontal scrolling, it might be desirable to still keep columns
  49. identifying each row visible all the time so that it's easier for the
  50. user to interpret the data. This can be done by freezing a number of
  51. columns, counted from the left, using the `setFrozenColumnCount(int)`
  52. method. By default, only the column showing selection state in
  53. multiselect mode is frozen. This column can also be unfrozen by setting
  54. the count to -1.
  55. [source,java]
  56. ....
  57. grid.setWidth("400px");
  58. grid.setFrozenColumnCount(1);
  59. grid.getColumn("name").setMinimumWidth(250);
  60. grid.getColumn("amount").setWidth(100);
  61. grid.getColumn("count").setWidth(100);
  62. ....
  63. If the width of Grid is again increased so that all columns can fit
  64. without scrolling, the frozen columns will behave just as any other
  65. column.