Class TableCell

All Implemented Interfaces:
AttachNotifier, DetachNotifier, HasComponents, HasComponentsOfType<Component>, HasElement, HasEnabled, HasSize, HasStyle, HasText, Serializable
Direct Known Subclasses:
TableDataCell, TableHeaderCell

@NullMarked public abstract class TableCell extends HtmlContainer
Base class for the two kinds of cell a TableRow can hold: a TableDataCell (<td>) and a TableHeaderCell (<th>). It exists so that operations that do not care which kind a cell is ? TableRow.getCells() and the row's inherited add and remove ? have a type to speak in.

Cells hold flow content, so this class does extend HtmlContainer: anything may go inside a cell.

Since:
25.3
See Also:
  • Constructor Details

    • TableCell

      protected TableCell()
      Creates a new empty cell.
    • TableCell

      protected TableCell(Component... components)
      Creates a new cell with the given children.
      Parameters:
      components - the children components.
    • TableCell

      protected TableCell(List<? extends Component> components)
      List equivalent of TableCell(Component...).
      Parameters:
      components - the children components.
  • Method Details

    • setColspan

      public void setColspan(int colspan)
      Sets the colspan attribute ? how many columns this cell spans. The default is 1.

      Unlike setRowspan(int), zero is not allowed: the "span the rest of the column group" meaning it once had was dropped from HTML, and browsers now clamp it back to 1. Browsers likewise clamp values above 1000 down to 1000 when laying the table out; the value is written through and read back verbatim here.

      Parameters:
      colspan - a positive integer.
      Throws:
      IllegalArgumentException - if colspan is less than 1.
    • getColspan

      public int getColspan()
      Returns the value of the colspan attribute. A cell with no colspan covers one column, so this reports 1 for it; use hasColspan() to tell that apart from an explicit colspan="1".
      Returns:
      the current colspan. Default is 1.
    • hasColspan

      public boolean hasColspan()
      Reports whether this cell carries an explicit colspan, as opposed to falling back to the default of 1.
      Returns:
      true if the attribute is set.
    • resetColspan

      public void resetColspan()
      Resets the colspan to its default value of 1.
    • setRowspan

      public void setRowspan(int rowspan)
      Sets the rowspan attribute ? how many rows this cell spans. The default is 1.

      Zero is allowed and still means something in HTML: the cell extends to the end of the row group (<thead>, <tbody> or <tfoot>, even an implicit one) it belongs to. Browsers clamp values above 65534 down to 65534 when laying the table out; the value is written through and read back verbatim here.

      Parameters:
      rowspan - a non-negative integer, where 0 spans the rest of the row group.
      Throws:
      IllegalArgumentException - if rowspan is negative.
    • getRowspan

      public int getRowspan()
      Returns the value of the rowspan attribute. A cell with no rowspan covers one row, so this reports 1 for it; use hasRowspan() to tell that apart from an explicit rowspan="1".
      Returns:
      the current rowspan. Default is 1.
    • hasRowspan

      public boolean hasRowspan()
      Reports whether this cell carries an explicit rowspan, as opposed to falling back to the default of 1. Note that rowspan="0", which reaches to the end of the row group, is an explicit span even though getRowspan() reports 0 rather than a row count.
      Returns:
      true if the attribute is set.
    • resetRowspan

      public void resetRowspan()
      Resets the rowspan to its default value of 1.
    • setHeaderIds

      public void setHeaderIds(String @Nullable ... ids)
      Sets the headers attribute ? a list of ids referring to the <th> cells that label this cell. Assistive technologies use it to read out the right headers when navigating complex tables, where scope alone isn't enough to disambiguate.

      An empty array removes the attribute, as does resetHeaders().

      Parameters:
      ids - the ids of the header cells, in any order. None may be blank or contain whitespace.
      Throws:
      IllegalArgumentException - if an id is blank or contains whitespace.
    • setHeaderIds

      public void setHeaderIds(@Nullable List<String> ids)
      List equivalent of setHeaderIds(String...). An empty list (or null) clears the attribute.
      Parameters:
      ids - the ids of the header cells, in any order. None may be blank or contain whitespace.
      Throws:
      IllegalArgumentException - if an id is blank or contains whitespace.
    • setHeaders

      public void setHeaders(TableHeaderCell @Nullable ... headerCells)
      Convenience overload that takes header cells directly and uses their id attributes. Each cell must have an id set.
      Parameters:
      headerCells - the header cells whose ids should be referenced.
      Throws:
      IllegalArgumentException - if any of the given cells does not have an id set.
    • setHeaders

      public void setHeaders(@Nullable List<? extends TableHeaderCell> headerCells)
      List equivalent of setHeaders(TableHeaderCell...).
      Parameters:
      headerCells - the header cells whose ids should be referenced.
      Throws:
      IllegalArgumentException - if any of the given cells does not have an id set.
    • getHeaderIds

      public List<String> getHeaderIds()
      Returns the ids of the header cells associated with this cell via the headers attribute, in the order they appear, or an empty list if the attribute is not set.
      Returns:
      the header ids, never null.
    • resetHeaders

      public void resetHeaders()
      Removes the headers attribute from this cell.