Class FormLayout
- All Implemented Interfaces:
AttachNotifier
,ClickNotifier<FormLayout>
,DetachNotifier
,HasComponents
,HasElement
,HasEnabled
,HasSize
,HasStyle
,Serializable
You can define how many columns Form Layout should use based on the screen width. A single column layout is preferable to a multi column one. A multi column layout is more prone to cause confusion and to be misinterpreted by the user. However, closely related fields can be placed in line without issue. For example, first and last name, address fields such as postal code and city, as well as ranged input for dates, time, currency, etc.
Best Practices:
Longer forms should be split into smaller, more manageable and user-friendly
sections using subheadings, Tabs, Details or separate views when possible.
Each section should consist of related content and/or fields.
Also, use the following guidelines for Button placement in forms:
- Buttons should be placed below the form they?re associated with.
- Buttons should be aligned left.
- Primary action first, followed by other actions, in order of importance.
Auto Responsive Mode
To avoid manually dealing with responsive breakpoints, Form Layout provides an auto-responsive mode that automatically creates and adjusts fixed-width columns based on the container's available space.
To control the number of columns and their widths, you can use the following properties:
columnWidth
- controls the column width (13em by default).maxColumns
- controls the maximum number of columns that the layout can create (10 by default).minColumns
- controls the minimum number of columns that the layout will create (1 by default).
The auto-responsive mode is disabled by default. To enable it for an
individual instance, set the autoResponsive
property to true
:
FormLayout formLayout = new FormLayout(); formLayout.setAutoResponsive(true); formLayout.add(new TextField("First name"), new TextField("Last name")); formLayout.add(new TextArea("Address"), 2); // colspan 2
You can also enable it for all instances by enabling the following feature
flag in src/main/resources/vaadin-featureflags.properties
:
com.vaadin.experimental.defaultAutoResponsiveFormLayout = true
Organizing Fields into Rows
By default, each field is placed on a new row. To organize fields into rows, you can either:
- Manually wrap fields into
FormLayout.FormRow
elements. - Enable the
autoRows
property to let Form Layout automatically arrange fields in available columns, wrapping to a new row when necessary. HTMLbr
elements can be used to force a new row.
Here is an example of using FormLayout.FormRow
:
FormLayout formLayout = new FormLayout(); formLayout.setAutoResponsive(true); FormRow firstRow = new FormRow(); firstRow.add(new TextField("First name"), new TextField("Last name")); FormRow secondRow = new FormRow(); secondRow.add(new TextArea("Address"), 2); // colspan 2 formLayout.add(firstRow, secondRow);
Expanding Columns and Fields
You can configure Form Layout to expand columns to evenly fill any remaining
space after all fixed-width columns have been created. To enable this, set
the expandColumns
property to
true
.
Also, Form Layout can stretch fields to make them take up all available space
within columns. To enable this, set the expandFields
property to true
.
Customizing Label Position
By default, Form Layout displays labels above the fields. To position labels
beside fields, you need to wrap each field in a FormLayout.FormItem
element and
define its labels on the wrapper. Then, you can enable the
labelsAside
property:
FormLayout formLayout = new FormLayout(); formLayout.setAutoResponsive(true); formLayout.setLabelsAside(true); FormRow firstRow = new FormRow(); firstRow.addFormItem(new TextField(), "First Name"); firstRow.addFormItem(new TextField(), "Last Name"); FormRow secondRow = new FormRow(); FormItem addressField = secondRow.addFormItem(new TextArea(), "Address"); secondRow.setColspan(addressField, 2); formLayout.add(firstRow, secondRow);
With this, FormLayout will display labels beside fields, falling back to the default position above the fields only when there isn't enough space.
- Author:
- Vaadin Ltd
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Server-side component for the<vaadin-form-item>
element.static class
Server-side component for the<vaadin-form-row>
element.static class
A class used in describing the responsive layouting behavior of aFormLayout
. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty layout.FormLayout
(Component... components) Constructs a FormLayout with the given initial components. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a component with the desired colspan.addFormItem
(Component field, Component label) Convenience method for creating and adding a new FormItem to this layout that wraps the given field with a component as its label.addFormItem
(Component field, String label) Convenience method for creating and adding a new FormItem to this layout that wraps the given field with a label.addFormRow
(Component... components) Convenience method dor creating and adding a newFormLayout.FormRow
to this layout.int
getColspan
(Component component) Gets the colspan of the given component.Gets the gap between the columns.Gets the width of columns that is used whensetAutoResponsive(boolean)
is enabled.Gets the gap between the label and the field which is used when labels are positioned aside.Gets the width of side-positioned label.int
Gets the maximum number of columns that the component can create when#setAutoResponsive(boolean)
is enabled.int
Gets the minimum number of columns that the component can create when#setAutoResponsive(boolean)
is enabled.Get the list ofFormLayout.ResponsiveStep
s used to configure this layout.Gets the gap between the rows.boolean
Gets whether the component is configured to automatically distribute fields across columns whensetAutoResponsive(boolean)
is enabled.boolean
Gets whether columns are configured to expand to fill remaining space whensetAutoResponsive(boolean)
is enabled.boolean
Gets whether fields are configured to stretch to take up all available space within columns whensetAutoResponsive(boolean)
is enabled.boolean
Gets whetherFormLayout.FormItem
is configured to prefer positioning labels beside the fields whensetAutoResponsive(boolean)
is enabled.void
setAutoResponsive
(boolean autoResponsive) When set totrue
, the component automatically creates and adjusts columns based on the container's width.void
setAutoRows
(boolean autoRows) Sets whether the component should automatically distribute fields across columns by placing each field in the next available column and wrapping to the next row when the current row is full.void
setColspan
(Component component, int colspan) Sets the colspan of the given component's element.void
setColumnSpacing
(float columnSpacing, Unit unit) Sets the gap between the columns.void
setColumnSpacing
(String columnSpacing) Sets the gap between the columns.void
setColumnWidth
(float columnWidth, Unit unit) Sets the width of columns that the component should use whensetAutoResponsive(boolean)
is enabled.void
setColumnWidth
(String columnWidth) Sets the width of columns that the component should use whensetAutoResponsive(boolean)
is enabled.void
setExpandColumns
(boolean expandColumns) Sets whether the columns should evenly expand in width to fill any remaining space after all columns have been created.void
setExpandFields
(boolean expandFields) Sets whether fields should stretch to take up all available space within columns.void
setLabelsAside
(boolean labelsAside) Sets whetherFormLayout.FormItem
should prefer positioning labels beside the fields.void
setLabelSpacing
(float labelSpacing, Unit unit) Sets the gap between the label and the field which is used when labels are positioned aside.void
setLabelSpacing
(String labelSpacing) Sets the gap between the label and the field which is used when labels are positioned aside.void
setLabelWidth
(float width, Unit unit) Sets the width of side-positioned label.void
setLabelWidth
(String width) Sets the width of side-positioned label.void
setMaxColumns
(int maxColumns) Sets the maximum number of columns that the component can create.void
setMinColumns
(int minColumns) Sets the minimum number of columns that the component will create.void
setResponsiveSteps
(FormLayout.ResponsiveStep... steps) Configure the responsive steps used in this layout.void
Configure the responsive steps used in this layout.void
setRowSpacing
(float rowSpacing, Unit unit) Sets the gap between the rows.void
setRowSpacing
(String rowSpacing) Sets the gap between the rows.Methods inherited from class com.vaadin.flow.component.Component
addListener, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisible
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListener
Methods inherited from interface com.vaadin.flow.component.ClickNotifier
addClickListener, addClickShortcut, addDoubleClickListener, addSingleClickListener
Methods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListener
Methods inherited from interface com.vaadin.flow.component.HasComponents
add, add, add, addComponentAsFirst, addComponentAtIndex, remove, remove, removeAll
Methods inherited from interface com.vaadin.flow.component.HasElement
getElement
Methods inherited from interface com.vaadin.flow.component.HasEnabled
isEnabled, setEnabled
Methods inherited from interface com.vaadin.flow.component.HasSize
getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFull
Methods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
-
Constructor Details
-
FormLayout
public FormLayout()Constructs an empty layout. Components can be added withHasComponents.add(Component...)
. -
FormLayout
Constructs a FormLayout with the given initial components. Additional components can be added after construction withHasComponents.add(Component...)
.- Parameters:
components
- the components to add- See Also:
-
-
Method Details
-
setColspan
Sets the colspan of the given component's element. Will default to 1 if an integer lower than 1 is supplied. You can directly add components with the wanted colspan withadd(Component, int)
.- Parameters:
component
- the component to set the colspan for, notnull
colspan
- the desired colspan for the component
-
add
Adds a component with the desired colspan. This method is a shorthand for callingHasComponents.add(Component...)
andsetColspan(Component, int)
- Parameters:
component
- the component to addcolspan
- the desired colspan for the component
-
getColspan
Gets the colspan of the given component. If none is set, returns 1.- Parameters:
component
- the component whose colspan is retrieved- Returns:
- the colspan of the given component or 1 if none is set
-
getResponsiveSteps
Get the list ofFormLayout.ResponsiveStep
s used to configure this layout.- Returns:
- the list of
FormLayout.ResponsiveStep
s used to configure this layout - See Also:
-
setResponsiveSteps
Configure the responsive steps used in this layout.NOTE: Responsive steps are ignored in auto-responsive mode, which may be enabled explicitly via
setAutoResponsive(boolean)
or implicitly if the following feature flag is set insrc/main/resources/vaadin-featureflags.properties
:com.vaadin.experimental.defaultAutoResponsiveFormLayout = true
- Parameters:
steps
- list ofFormLayout.ResponsiveStep
s to set- See Also:
-
setResponsiveSteps
Configure the responsive steps used in this layout.NOTE: Responsive steps are ignored in auto-responsive mode, which may be enabled explicitly via
setAutoResponsive(boolean)
or implicitly if the following feature flag is set insrc/main/resources/vaadin-featureflags.properties
:com.vaadin.experimental.defaultAutoResponsiveFormLayout = true
- Parameters:
steps
- theFormLayout.ResponsiveStep
s to set- See Also:
-
addFormItem
Convenience method for creating and adding a new FormItem to this layout that wraps the given field with a label. Shorthand foraddFormItem(field, new Label(label))
.- Parameters:
field
- the field component to wraplabel
- the label text to set- Returns:
- the created form item
- See Also:
-
addFormItem
Convenience method for creating and adding a new FormItem to this layout that wraps the given field with a component as its label.- Parameters:
field
- the field component to wraplabel
- the label component to set- Returns:
- the created form item
-
addFormRow
Convenience method dor creating and adding a newFormLayout.FormRow
to this layout. The method accepts a list of components that will be added to the row.- Parameters:
components
- the components to add to the row- Returns:
- the created form row
-
setLabelWidth
Sets the width of side-positioned label.- Parameters:
width
- the value and CSS unit as a string- See Also:
-
setLabelWidth
Sets the width of side-positioned label.- Parameters:
width
- the value of the widthunit
- the CSS unit of the width- See Also:
-
getLabelWidth
Gets the width of side-positioned label.- Returns:
- the value and CSS unit as a string
- See Also:
-
setLabelSpacing
Sets the gap between the label and the field which is used when labels are positioned aside. The value must be provided in CSS length units, e.g.1em
.- Parameters:
labelSpacing
- the gap between the label and the field- See Also:
-
setLabelSpacing
Sets the gap between the label and the field which is used when labels are positioned aside. The value must be provided with aUnit
, e.g.,1
andUnit.EM
.- Parameters:
labelSpacing
- the gap between the label and the fieldunit
- the CSS unit of the gap- See Also:
-
getLabelSpacing
Gets the gap between the label and the field which is used when labels are positioned aside.- Returns:
- the value and CSS unit as a string
- See Also:
-
setColumnSpacing
Sets the gap between the columns. The value must be provided in CSS length units, e.g.1em
.- Parameters:
columnSpacing
- the gap between the columns- See Also:
-
setColumnSpacing
- Parameters:
columnSpacing
- the gap between the columnsunit
- the CSS unit of the gap- See Also:
-
getColumnSpacing
Gets the gap between the columns.- Returns:
- the value and CSS unit as a string
- See Also:
-
setRowSpacing
Sets the gap between the rows. The value must be provided in CSS length units, e.g.1em
.- Parameters:
rowSpacing
- the gap between the rows- See Also:
-
setRowSpacing
- Parameters:
rowSpacing
- the gap between the rowsunit
- the CSS unit of the gap- See Also:
-
getRowSpacing
Gets the gap between the rows.- Returns:
- the value and CSS unit as a string
- See Also:
-
setAutoResponsive
public void setAutoResponsive(boolean autoResponsive) When set totrue
, the component automatically creates and adjusts columns based on the container's width. Columns have a fixed width defined bysetColumnWidth(String)
. The number of columns increases up to the limit set bysetMaxColumns(int)
. The minimum number of columns will be created is set bysetMinColumns(int)
. The component dynamically adjusts the number of columns as the container size changes. When this mode is enabled,Responsive steps
are ignored.By default, each field is placed on a new row. To organize fields into rows, there are two options:
- Use
FormLayout.FormRow
to explicitly group fields into rows. - Enable the
setAutoRows(boolean)
property to automatically arrange fields in available columns, wrapping to a new row when necessary.ElementFactory.createBr()
elements can be used to force a new row.
The auto-responsive mode is disabled by default. To enable it for an individual instance, use this method. Alternatively, if you want it to be enabled for all instances by default, enable the following feature flag in
src/main/resources/vaadin-featureflags.properties
:com.vaadin.experimental.defaultAutoResponsiveFormLayout = true
- Parameters:
autoResponsive
-true
to enable auto responsive mode,false
to disable
- Use
-
setAutoRows
public void setAutoRows(boolean autoRows) Sets whether the component should automatically distribute fields across columns by placing each field in the next available column and wrapping to the next row when the current row is full.ElementFactory.createBr()
elements can be used to force a new row.The default value is
false
.This setting only applies when
setAutoResponsive(boolean)
is enabled.- Parameters:
autoRows
-true
to enable auto rows mode,false
otherwise
-
isAutoRows
public boolean isAutoRows()Gets whether the component is configured to automatically distribute fields across columns whensetAutoResponsive(boolean)
is enabled.- Returns:
true
if auto rows mode is enabled,false
otherwise- See Also:
-
setColumnWidth
Sets the width of columns that the component should use whensetAutoResponsive(boolean)
is enabled. The value must be provided in CSS length units, e.g.100px
.When the column width is
null
, the web component defaults to12em
or uses the value of--vaadin-field-default-width
if that CSS custom property is defined.This setting only applies when
setAutoResponsive(boolean)
is enabled.- Parameters:
columnWidth
- the width of columns ornull
to use the default
-
setColumnWidth
Sets the width of columns that the component should use whensetAutoResponsive(boolean)
is enabled. The value must be provided with aUnit
, e.g.100
andUnit.PIXELS
.When the column width is
null
, the web component defaults to12em
or uses the value of--vaadin-field-default-width
if that CSS custom property is defined.This setting only applies when
setAutoResponsive(boolean)
is enabled.- Parameters:
columnWidth
- the width of columnsunit
- the CSS unit of the width
-
getColumnWidth
Gets the width of columns that is used whensetAutoResponsive(boolean)
is enabled.When the column width is
null
, the web component defaults to12em
or uses the value of--vaadin-field-default-width
if that CSS custom property is defined.- Returns:
- the value and CSS unit as a string, or
null
if not set, in which case the web component uses its default value - See Also:
-
setMaxColumns
public void setMaxColumns(int maxColumns) Sets the maximum number of columns that the component can create. The component will create columns up to this limit based on the available container width.By default, the web component uses a maximum of 10 columns.
This setting only applies when
setAutoResponsive(boolean)
is enabled.- Parameters:
maxColumns
- the maximum number of columns
-
getMaxColumns
public int getMaxColumns()Gets the maximum number of columns that the component can create when#setAutoResponsive(boolean)
is enabled.- Returns:
- the maximum number of columns or 0 if not explicitly set
- See Also:
-
setMinColumns
public void setMinColumns(int minColumns) Sets the minimum number of columns that the component will create.By default, the web component uses a minimum of 1 column.
This setting only applies when
setAutoResponsive(boolean)
is enabled.- Parameters:
minColumns
- the minimum number of columns
-
getMinColumns
public int getMinColumns()Gets the minimum number of columns that the component can create when#setAutoResponsive(boolean)
is enabled.- Returns:
- the minimum number of columns or 0 if not explicitly set
- See Also:
-
setExpandColumns
public void setExpandColumns(boolean expandColumns) Sets whether the columns should evenly expand in width to fill any remaining space after all columns have been created.The default value is
false
.This setting only applies when
setAutoResponsive(boolean)
is enabled.- Parameters:
expandColumns
-true
to expand columns,false
otherwise
-
isExpandColumns
public boolean isExpandColumns()Gets whether columns are configured to expand to fill remaining space whensetAutoResponsive(boolean)
is enabled.- Returns:
true
if columns should expand,false
otherwise- See Also:
-
setExpandFields
public void setExpandFields(boolean expandFields) Sets whether fields should stretch to take up all available space within columns. Fields insideFormLayout.FormItem
elements are also included.The default value is
false
.This setting only applies when
setAutoResponsive(boolean)
is enabled.- Parameters:
expandFields
-true
to expand fields,false
otherwise
-
isExpandFields
public boolean isExpandFields()Gets whether fields are configured to stretch to take up all available space within columns whensetAutoResponsive(boolean)
is enabled.- Returns:
true
if fields should expand,false
otherwise- See Also:
-
setLabelsAside
public void setLabelsAside(boolean labelsAside) Sets whetherFormLayout.FormItem
should prefer positioning labels beside the fields. If the layout is too narrow to fit a single column with a side label, labels will automatically switch to their default position above the fields until the layout gets wide again.This setting only applies when
setAutoResponsive(boolean)
is enabled.To customize the label width and the gap between the label and the field, use the following methods:
Alternatively, you can use the following CSS custom properties:
--vaadin-form-layout-label-width
--vaadin-form-layout-label-spacing
- Parameters:
labelsAside
-true
to position labels aside,false
otherwise
-
isLabelsAside
public boolean isLabelsAside()Gets whetherFormLayout.FormItem
is configured to prefer positioning labels beside the fields whensetAutoResponsive(boolean)
is enabled.- Returns:
true
if labels are positioned aside,false
otherwise- See Also:
-