Class Table<T extends Table<T>>

All Implemented Interfaces:
com.oorian.css.TableIntf<T>

public class Table<T extends Table<T>> extends StyledElement<T> implements com.oorian.css.TableIntf<T>
Represents the HTML <table> element for displaying tabular data.

The Table class provides a Java representation of the HTML table element, which creates structured data tables with rows and cells. It supports complete table structure including header (thead), body (tbody), and footer (tfoot) sections, along with comprehensive CSS table styling capabilities.

Features:

  • Structured table sections: header, body, and footer
  • Dynamic row and cell management
  • Border collapse and spacing control
  • Table layout algorithm configuration (auto/fixed)
  • Caption positioning control
  • Empty cells display handling
  • Caption support for table titles
  • Column groups for column-level styling
  • Sortable table support
  • Flexible styling for all table sections
  • Programmatic row access and manipulation

Usage Example:


 // Create a basic data table
 Table table = new Table();
 table.setBorderCollapse(BorderCollapse.COLLAPSE);
 table.setWidth("100%");

 // Add header row
 Tr headerRow = table.getHeader().addRow();
 headerRow.addHeaderCell("Name");
 headerRow.addHeaderCell("Email");
 headerRow.addHeaderCell("Role");

 // Add data rows
 Tr dataRow = table.addRow();
 dataRow.addDataCell("John Doe");
 dataRow.addDataCell("john@example.com");
 dataRow.addDataCell("Administrator");

 // Add a caption
 table.setCaption("Monthly Sales Report");

 // Add column groups
 Colgroup colgroup = new Colgroup();
 colgroup.addCol(new Col(1));
 colgroup.addCol(new Col(2));
 table.addColgroup(colgroup);

 // Configure table layout
 table.setTableLayout(TableLayout.FIXED);
 table.setBorderSpacing(5);
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Field Details

  • Constructor Details

    • Table

      public Table()
      Creates a new Table element with default settings. Initializes with zero cellpadding/cellspacing and creates empty header, body, and footer sections.
  • Method Details

    • addElement

      public T addElement(Element child)
      Adds a child element to this table.

      If the child is a Thead, Tbody, or Tfoot, it replaces the existing section of the same type at the same position in the child list. This ensures that parsed or externally created table sections integrate correctly with the pre-created header, body, and footer sections.

      Overrides:
      addElement in class Element<T extends Table<T>>
      Parameters:
      child - the child element to add.
      Returns:
      this element for method chaining
    • setBorder

      public void setBorder(boolean flag)
      Sets whether the table displays a border.
      Parameters:
      flag - true to display a border, false to hide it
    • setSortable

      public void setSortable(boolean sortable)
      Sets whether the table is sortable by clicking on column headers.
      Parameters:
      sortable - true to enable sorting, false to disable
    • setBorderCollapse

      public final T setBorderCollapse(String borderCollapse)
      Sets the border-collapse property using a string value.
      Specified by:
      setBorderCollapse in interface com.oorian.css.TableIntf<T extends Table<T>>
      Parameters:
      borderCollapse - the CSS value (e.g., "collapse", "separate")
      Returns:
      this for method chaining
    • setBorderCollapse

      public T setBorderCollapse(BorderCollapse attrValue)
      Sets the border-collapse property using an enum value.
      Specified by:
      setBorderCollapse in interface com.oorian.css.TableIntf<T extends Table<T>>
      Parameters:
      attrValue - the BorderCollapse enum value
      Returns:
      this for method chaining
    • setBorderSpacing

      public final T setBorderSpacing(String spacing)
      Sets the border-spacing property using a string value.
      Specified by:
      setBorderSpacing in interface com.oorian.css.TableIntf<T extends Table<T>>
      Parameters:
      spacing - the CSS value (e.g., "5px", "2px 4px")
      Returns:
      this for method chaining
    • setBorderSpacing

      public final T setBorderSpacing(int pixels)
      Sets the border-spacing property using a pixel value.
      Specified by:
      setBorderSpacing in interface com.oorian.css.TableIntf<T extends Table<T>>
      Parameters:
      pixels - the spacing in pixels for both horizontal and vertical
      Returns:
      this for method chaining
    • setBorderSpacing

      public T setBorderSpacing(String hspacing, String vspacing)
      Sets the border-spacing property with separate horizontal and vertical string values.
      Specified by:
      setBorderSpacing in interface com.oorian.css.TableIntf<T extends Table<T>>
      Parameters:
      hspacing - the horizontal spacing value
      vspacing - the vertical spacing value
      Returns:
      this for method chaining
    • setBorderSpacing

      public T setBorderSpacing(int hspacing, int vspacing)
      Sets the border-spacing property with separate horizontal and vertical pixel values.
      Specified by:
      setBorderSpacing in interface com.oorian.css.TableIntf<T extends Table<T>>
      Parameters:
      hspacing - the horizontal spacing in pixels
      vspacing - the vertical spacing in pixels
      Returns:
      this for method chaining
    • setBorderSpacing

      public T setBorderSpacing(Units units)
      Sets the border-spacing property using a Units object.
      Specified by:
      setBorderSpacing in interface com.oorian.css.TableIntf<T extends Table<T>>
      Parameters:
      units - the Units object specifying the spacing
      Returns:
      this for method chaining
    • setBorderSpacing

      public T setBorderSpacing(Units hspacing, Units vspacing)
      Sets the border-spacing property with separate horizontal and vertical Units values.
      Specified by:
      setBorderSpacing in interface com.oorian.css.TableIntf<T extends Table<T>>
      Parameters:
      hspacing - the Units object for horizontal spacing
      vspacing - the Units object for vertical spacing
      Returns:
      this for method chaining
    • setCaptionSide

      public final T setCaptionSide(String attrValue)
      Sets the caption-side property using a string value.
      Specified by:
      setCaptionSide in interface com.oorian.css.TableIntf<T extends Table<T>>
      Parameters:
      attrValue - the CSS value (e.g., "top", "bottom")
      Returns:
      this for method chaining
    • setCaptionSide

      public T setCaptionSide(CaptionSide attrValue)
      Sets the caption-side property using an enum value.
      Specified by:
      setCaptionSide in interface com.oorian.css.TableIntf<T extends Table<T>>
      Parameters:
      attrValue - the CaptionSide enum value specifying caption placement
      Returns:
      this for method chaining
    • setEmptyCells

      public final T setEmptyCells(String attrValue)
      Sets the empty-cells property using a string value.
      Specified by:
      setEmptyCells in interface com.oorian.css.TableIntf<T extends Table<T>>
      Parameters:
      attrValue - the CSS value (e.g., "show", "hide")
      Returns:
      this for method chaining
    • setEmptyCells

      public T setEmptyCells(EmptyCells attrValue)
      Sets the empty-cells property using an enum value.
      Specified by:
      setEmptyCells in interface com.oorian.css.TableIntf<T extends Table<T>>
      Parameters:
      attrValue - the EmptyCells enum value specifying how empty cells are rendered
      Returns:
      this for method chaining
    • setTableLayout

      public final T setTableLayout(String attrValue)
      Sets the table-layout property using a string value.
      Specified by:
      setTableLayout in interface com.oorian.css.TableIntf<T extends Table<T>>
      Parameters:
      attrValue - the CSS value (e.g., "auto", "fixed")
      Returns:
      this for method chaining
    • setTableLayout

      public final T setTableLayout(TableLayout attrValue)
      Sets the table-layout property using an enum value.
      Specified by:
      setTableLayout in interface com.oorian.css.TableIntf<T extends Table<T>>
      Parameters:
      attrValue - the TableLayout enum value specifying the layout algorithm
      Returns:
      this for method chaining
    • setCaption

      public void setCaption(Caption caption)
      Sets the table caption, replacing any existing caption.

      The caption is always rendered as the first child of the table element, before any column groups and table sections, as required by the HTML specification.

      Parameters:
      caption - the Caption element to set
    • setCaption

      public void setCaption(String text)
      Sets the table caption with the specified text, replacing any existing caption.

      This is a convenience method that creates a Caption element with the given text.

      Parameters:
      text - the caption text
    • getCaption

      public Caption getCaption()
      Returns the table caption, or null if no caption has been set.
      Returns:
      the Caption element, or null
    • addColgroup

      public void addColgroup(Colgroup colgroup)
      Adds a column group to this table.

      Column groups are inserted after the caption (if present) and before the table sections (thead, tbody, tfoot), as required by the HTML specification.

      Parameters:
      colgroup - the Colgroup element to add
    • setHeaderStyle

      public void setHeaderStyle(CssStyle style)
      Sets the style for the table header section using a CssStyle object.
      Parameters:
      style - the CssStyle object to apply to the header
    • setFooterStyle

      public void setFooterStyle(CssStyle style)
      Sets the style for the table footer section using a CssStyle object.
      Parameters:
      style - the CssStyle object to apply to the footer
    • setHeaderStyle

      public void setHeaderStyle(String cssClass)
      Sets the CSS class for the table header section.
      Parameters:
      cssClass - the CSS class name to apply to the header
    • setFooterStyle

      public void setFooterStyle(String cssClass)
      Sets the CSS class for the table footer section.
      Parameters:
      cssClass - the CSS class name to apply to the footer
    • getHeader

      public Thead getHeader()
      Returns the table header (thead) section.
      Returns:
      the Thead element for this table
    • getBody

      public Tbody getBody()
      Returns the table body (tbody) section.
      Returns:
      the Tbody element for this table
    • getFooter

      public Tfoot getFooter()
      Returns the table footer (tfoot) section.
      Returns:
      the Tfoot element for this table
    • addRow

      public Tr addRow()
      Creates and adds a new row to the table body.
      Returns:
      the newly created Tr element
    • addRow

      public void addRow(Tr row)
      Adds an existing row to the table body.
      Parameters:
      row - the Tr element to add
    • removeRow

      public void removeRow(int index)
      Removes a row from the table body at the specified index.
      Parameters:
      index - the zero-based index of the row to remove
    • removeRows

      public void removeRows(int start, int end)
      Removes a range of rows from the table body.
      Parameters:
      start - the starting index (inclusive)
      end - the ending index (exclusive)
    • removeAllRows

      public void removeAllRows()
      Removes all rows from the table body.
    • getRow

      public Tr getRow(int index)
      Returns the row at the specified index in the table body.
      Parameters:
      index - the zero-based index of the row
      Returns:
      the Tr element at the specified index, or null if not found