Class BeanTable<T>

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.HtmlComponent
org.vaadin.tatu.BeanTable<T>
Type Parameters:
T - Bean type for the Table
All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasStyle, com.vaadin.flow.component.HasTheme, com.vaadin.flow.data.provider.HasDataView<T,Void,BeanTableDataView<T>>, com.vaadin.flow.data.provider.HasLazyDataView<T,Void,BeanTableLazyDataView<T>>, com.vaadin.flow.data.provider.HasListDataView<T,BeanTableListDataView<T>>, Serializable

@StyleSheet("bean-table.css") @Tag("table") public class BeanTable<T> extends com.vaadin.flow.component.HtmlComponent implements com.vaadin.flow.data.provider.HasListDataView<T,BeanTableListDataView<T>>, com.vaadin.flow.data.provider.HasDataView<T,Void,BeanTableDataView<T>>, com.vaadin.flow.data.provider.HasLazyDataView<T,Void,BeanTableLazyDataView<T>>, com.vaadin.flow.component.HasTheme
This is a simple Table component backed by DataProvider. The data provider populates the Table with data from the beans. The component has minimal API and ultra simple design. The purpose of this component is to be a little sibling to Grid. Thus there are many features intentionally left out. This component does support lazy loading of the data and uses paging for it. Table's cells can be populated by text, html or components. A11y supported. Theme variants for stripes, padding, wrapping, border styles provided. Component has css class name "bean-table" and custom css can be applied with it. Furthermore there is class name provider API.
Author:
Tatu Lund
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Localization object for BeanTable
    class 
    Configuration class for the Columns.
    static enum 
     
    static enum 
     
    static interface 
     
    static enum 
     
    static interface 
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    The default constructor.
    BeanTable(int pageLength)
    The default constructor with defined page length.
    BeanTable(Class<T> beanType)
    Creates a new BeanTable with an initial set of columns for each of the bean's properties.
    BeanTable(Class<T> beanType, boolean autoCreateColumns)
    Creates a new BeanTable with an initial set of columns for each of the bean's properties.
    BeanTable(Class<T> beanType, boolean autoCreateColumns, int pageLength)
    Creates a new BeanTable with an initial set of columns for each of the bean's properties.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addColumn(String propertyName)
    Add column to Table with the given property name.
    addColumn(String header, com.vaadin.flow.function.ValueProvider<T,?> valueProvider)
    Add a column with a value provider.
    Add a column with component provider.
    com.vaadin.flow.shared.Registration
    addItemClickedListener(com.vaadin.flow.component.ComponentEventListener<ItemClickedEvent<T,BeanTable<T>>> listener)
    Add ItemClickedEvent listener to the BeanTable
    com.vaadin.flow.shared.Registration
    addSelectionChangedListener(com.vaadin.flow.component.ComponentEventListener<BeanTableSelectionChangedEvent<T,BeanTable<T>>> listener)
    Add SelectionChangedEvent listener to the BeanTable
    void
    Adds theme variants to the component.
    void
    deselect(T... items)
    Deselect items.
    void
    Clear the selection.
    protected T
    fetchItem(int index)
     
    void
    Push focus to first body cell, not row header cell.
    void
    focus(int row, int col)
    Push focus to specific row and column by index if such cell exists in the table.
    void
    focus(T item)
    Push focus to the first column of the row where item is if item is on the current page.
     
    Get the column by its key.
    Get list of the currently set columns.
    com.vaadin.flow.data.provider.DataProvider<T,?>
    Return the currently used data provider.
     
    Gets the internationalization object previously set for this component.
     
     
    int
    Get current page.
    int
    Get the number of the rows in the table after filtering.
    Get currently selected items.
    protected void
    onAttach(com.vaadin.flow.component.AttachEvent attachEvent)
     
    protected void
    onDetach(com.vaadin.flow.component.DetachEvent detachEvent)
     
    void
    Removes theme variants from the component.
    void
    select(T... items)
    Select items.
    void
    setCaption(String caption)
    Set the caption associated with the Table
    void
    Set class name provider for a table row.
    void
    setColumns(String... propertyNames)
    Configure BeanTable to have columns with given set of property names.
    void
    Use ColumnSelectMenu.CONTEXT column selection as context menu.
    void
    setDataProvider(com.vaadin.flow.data.provider.DataProvider<T,?> dataProvider)
    Deprecated.
    void
    Set the focus behavior of this table for A11y.
    void
    setHtmlAllowed(boolean htmlAllowed)
    Set whether cell content should allow html content or not.
    void
    Sets the internationalization properties (texts used for button tooltips) for this component.
    setItems(com.vaadin.flow.data.provider.BackEndDataProvider<T,Void> dataProvider)
     
    setItems(com.vaadin.flow.data.provider.DataProvider<T,Void> dataProvider)
     
    setItems(com.vaadin.flow.data.provider.InMemoryDataProvider<T> inMemoryDataProvider)
     
    setItems(com.vaadin.flow.data.provider.ListDataProvider<T> dataProvider)
     
    void
    setItems(Stream<T> streamOfItems)
     
    void
    setPage(int page)
    Set the currently shown page.
    void
    setSelectionEnabled(boolean selectionEnabled)
    Enable/disable selection for user.

    Methods inherited from class com.vaadin.flow.component.HtmlComponent

    getTitle, setTitle

    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, 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.DetachNotifier

    addDetachListener

    Methods inherited from interface com.vaadin.flow.component.HasElement

    getElement

    Methods inherited from interface com.vaadin.flow.data.provider.HasLazyDataView

    setItems, setItems

    Methods inherited from interface com.vaadin.flow.data.provider.HasListDataView

    setItems, setItems

    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

    Methods inherited from interface com.vaadin.flow.component.HasTheme

    addThemeName, addThemeNames, getThemeName, getThemeNames, hasThemeName, removeThemeName, removeThemeNames, setThemeName, setThemeName
  • Constructor Details

    • BeanTable

      public BeanTable()
      The default constructor. This creates a BeanTable without further configuration. Use addColumn(String,ValueProvider) addComponentColumn(String,ComponentProvider) to configure columns.
    • BeanTable

      public BeanTable(int pageLength)
      The default constructor with defined page length. Use this constructor with large data sources, i.e. DataProvider.fromCallBacks(..). This constructor enables paging controls in the footer row. Also this creates a BeanTable without further configuration. Use addColumn(String,ValueProvider) addComponentColumn(String,ComponentProvider) to configure columns.
      Parameters:
      pageLength - Size of the page
    • BeanTable

      public BeanTable(Class<T> beanType)
      Creates a new BeanTable with an initial set of columns for each of the bean's properties. The property-values of the bean will be converted to Strings. Full names of the properties will be used as the header captions.
      Parameters:
      beanType - the bean type to use, not null
    • BeanTable

      public BeanTable(Class<T> beanType, boolean autoCreateColumns)
      Creates a new BeanTable with an initial set of columns for each of the bean's properties. The property-values of the bean will be converted to Strings. Full names of the properties will be used as the header captions.

      Constructor with defined page length. Use this constructor with large data sources, i.e. DataProvider.fromCallBacks(..). This constructor enables paging controls in the footer row.

      When autoCreateColumns is false. Use setColumns(String...) to define which properties to include and in which order. You can also add a column for an individual property with addColumn(String).

      Parameters:
      beanType - the bean type to use, not null
      autoCreateColumns - when true, columns are created automatically for the properties of the beanType
    • BeanTable

      public BeanTable(Class<T> beanType, boolean autoCreateColumns, int pageLength)
      Creates a new BeanTable with an initial set of columns for each of the bean's properties. The property-values of the bean will be converted to Strings. Full names of the properties will be used as the header captions.

      When autoCreateColumns is false. Use setColumns(String...) to define which properties to include and in which order. You can also add a column for an individual property with addColumn(String).

      Parameters:
      beanType - the bean type to use, not null
      autoCreateColumns - when true, columns are created automatically for the properties of the beanType
      pageLength - Size of the page
  • Method Details

    • addColumn

      public void addColumn(String propertyName)
      Add column to Table with the given property name.
      Parameters:
      propertyName - The property
    • setColumns

      public void setColumns(String... propertyNames)
      Configure BeanTable to have columns with given set of property names.
      Parameters:
      propertyNames - List of property names
    • addColumn

      public BeanTable<T>.Column<T> addColumn(String header, com.vaadin.flow.function.ValueProvider<T,?> valueProvider)
      Add a column with a value provider. Value provider is a function reference, e.g. getter of the bean or lambda that returns the value for this column.
      Parameters:
      header - The header as a string, can be null
      valueProvider - The value provider
      Returns:
      A column
    • addComponentColumn

      public BeanTable<T>.Column<T> addComponentColumn(String header, BeanTable.ComponentProvider<T> componentProvider)
      Add a column with component provider. Component provider is a lambda that must return a new instance of a component.
      Parameters:
      header - Header as string, can be null
      componentProvider - Component provider
      Returns:
      A column
    • setDataProvider

      @Deprecated public void setDataProvider(com.vaadin.flow.data.provider.DataProvider<T,?> dataProvider)
      Deprecated.
    • fetchItem

      protected T fetchItem(int index)
    • getDataProvider

      public com.vaadin.flow.data.provider.DataProvider<T,?> getDataProvider()
      Return the currently used data provider.
      Returns:
      A data provider
    • onAttach

      protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent)
      Overrides:
      onAttach in class com.vaadin.flow.component.Component
    • onDetach

      protected void onDetach(com.vaadin.flow.component.DetachEvent detachEvent)
      Overrides:
      onDetach in class com.vaadin.flow.component.Component
    • setHtmlAllowed

      public void setHtmlAllowed(boolean htmlAllowed)
      Set whether cell content should allow html content or not. If this is false (default), value will be set as text content of the cell. If set to true the value string will be wrapped in span element and can contain html.
      Parameters:
      htmlAllowed - A boolean value.
    • setClassNameProvider

      public void setClassNameProvider(BeanTable.StringProvider<T> classNameProvider)
      Set class name provider for a table row.
      Parameters:
      classNameProvider - StringProvider Lambda callback bean instance to String.
    • getClassNameProvider

      public BeanTable.StringProvider<T> getClassNameProvider()
    • getGenericDataView

      public BeanTableDataView<T> getGenericDataView()
      Specified by:
      getGenericDataView in interface com.vaadin.flow.data.provider.HasDataView<T,Void,BeanTableDataView<T>>
    • setItems

      public BeanTableDataView<T> setItems(com.vaadin.flow.data.provider.DataProvider<T,Void> dataProvider)
      Specified by:
      setItems in interface com.vaadin.flow.data.provider.HasDataView<T,Void,BeanTableDataView<T>>
    • setItems

      public BeanTableDataView<T> setItems(com.vaadin.flow.data.provider.InMemoryDataProvider<T> inMemoryDataProvider)
      Specified by:
      setItems in interface com.vaadin.flow.data.provider.HasDataView<T,Void,BeanTableDataView<T>>
    • getListDataView

      public BeanTableListDataView<T> getListDataView()
      Specified by:
      getListDataView in interface com.vaadin.flow.data.provider.HasListDataView<T,BeanTableListDataView<T>>
    • setItems

      public BeanTableListDataView<T> setItems(com.vaadin.flow.data.provider.ListDataProvider<T> dataProvider)
      Specified by:
      setItems in interface com.vaadin.flow.data.provider.HasListDataView<T,BeanTableListDataView<T>>
    • setItems

      public void setItems(Stream<T> streamOfItems)
    • setItems

      public BeanTableLazyDataView<T> setItems(com.vaadin.flow.data.provider.BackEndDataProvider<T,Void> dataProvider)
      Specified by:
      setItems in interface com.vaadin.flow.data.provider.HasLazyDataView<T,Void,BeanTableLazyDataView<T>>
    • getLazyDataView

      public BeanTableLazyDataView<T> getLazyDataView()
      Specified by:
      getLazyDataView in interface com.vaadin.flow.data.provider.HasLazyDataView<T,Void,BeanTableLazyDataView<T>>
    • getColumns

      public List<BeanTable<T>.Column<T>> getColumns()
      Get list of the currently set columns.
      Returns:
      List of Columns
    • getColumn

      public Optional<BeanTable<T>.Column<T>> getColumn(String key)
      Get the column by its key.
      Parameters:
      key - The key, can't be null
      Returns:
      Optional Column
    • setCaption

      public void setCaption(String caption)
      Set the caption associated with the Table
      Parameters:
      caption - A string value. Null will remove caption.
    • setPage

      public void setPage(int page)
      Set the currently shown page.
      Parameters:
      page - int value base 0
      Throws:
      IllegalArgumentException - if BeanTable is not in paged mode or page is not in acceptable range.
    • getPage

      public int getPage()
      Get current page.
      Returns:
      int value base 0, or -1 if BeanTable is not paged mode.
    • getRowCount

      public int getRowCount()
      Get the number of the rows in the table after filtering.
      Returns:
      int value.
    • setI18n

      public void setI18n(BeanTable.BeanTableI18n i18n)
      Sets the internationalization properties (texts used for button tooltips) for this component.
      Parameters:
      i18n - the internationalized properties, null to disable all tooltips.
    • getI18n

      public BeanTable.BeanTableI18n getI18n()
      Gets the internationalization object previously set for this component.
      Returns:
      the i18n object. It will be null, If the i18n properties weren't set.
    • focus

      public void focus()
      Push focus to first body cell, not row header cell.

      Note: If FocusBehavior.NONE used, then does nothing.

    • focus

      public void focus(T item)
      Push focus to the first column of the row where item is if item is on the current page.

      Note: If FocusBehavior.NONE used, then does nothing.

      Parameters:
      item - The item to focus.
    • focus

      public void focus(int row, int col)
      Push focus to specific row and column by index if such cell exists in the table.

      Note: If FocusBehavior.NONE used, then does nothing.

      Parameters:
      row - int value
      col - int value
    • setFocusBehavior

      public void setFocusBehavior(BeanTable.FocusBehavior focusBehavior)
      Set the focus behavior of this table for A11y. NONE is the default and table is skipped in focus tabbing. When BODY is used only body cells gets focus. When BODY_AND_HEADER is used also header cells get focus.
      Parameters:
      focusBehavior - FocusBehavior
    • addThemeVariants

      public void addThemeVariants(BeanTableVariant... variants)
      Adds theme variants to the component.
      Parameters:
      variants - theme variants to add
    • removeThemeVariants

      public void removeThemeVariants(BeanTableVariant... variants)
      Removes theme variants from the component.
      Parameters:
      variants - theme variants to remove
    • setColumnSelectionMenu

      public void setColumnSelectionMenu(BeanTable.ColumnSelectMenu columnSelect)
      Use ColumnSelectMenu.CONTEXT column selection as context menu. Use ColumnSelectMenu.BUTTON to column selection button to open the menu in the last header cell.
      Parameters:
      columnSelect - ColumnSelectMenu
    • getSelected

      public Set<T> getSelected()
      Get currently selected items.
      Returns:
      Set of selected items.
    • select

      public void select(T... items)
      Select items.
      Parameters:
      items - Items to be selected.
    • deselect

      public void deselect(T... items)
      Deselect items.
      Parameters:
      items - Items to be deselected.
    • deselectAll

      public void deselectAll()
      Clear the selection.
    • setSelectionEnabled

      public void setSelectionEnabled(boolean selectionEnabled)
      Enable/disable selection for user.

      Note: If turned on, FocusBehavior is set to FocusBehavior.BODY_AND_HEADER to allow keyboard controls.

      Parameters:
      selectionEnabled - Boolean value.
    • addSelectionChangedListener

      public com.vaadin.flow.shared.Registration addSelectionChangedListener(com.vaadin.flow.component.ComponentEventListener<BeanTableSelectionChangedEvent<T,BeanTable<T>>> listener)
      Add SelectionChangedEvent listener to the BeanTable
      Parameters:
      listener - the listener to add.
      Returns:
      a registration for the listener
    • addItemClickedListener

      public com.vaadin.flow.shared.Registration addItemClickedListener(com.vaadin.flow.component.ComponentEventListener<ItemClickedEvent<T,BeanTable<T>>> listener)
      Add ItemClickedEvent listener to the BeanTable
      Parameters:
      listener - the listener to add.
      Returns:
      a registration for the listener