Class Stack<T extends Stack<T>>

Type Parameters:
T - the concrete subtype for fluent method chaining
Direct Known Subclasses:
HStack, VStack

public abstract class Stack<T extends Stack<T>> extends StyledContainerElement<T>
A flexible box layout container that arranges children in a single direction with configurable spacing.

Stack is the base class for layout containers that use CSS Flexbox to arrange their children in either a horizontal row or vertical column. It provides a simplified API for common flexbox operations like setting gaps, alignment, and wrapping behavior.

Features:

  • Automatic flex display mode
  • Configurable gap between children
  • Cross-axis and main-axis alignment
  • Optional wrapping for overflow content

Usage:


 // Vertical stack with 16px gap
 VStack vstack = new VStack();
 vstack.setGap(16);
 vstack.addElement(new Div("Item 1"));
 vstack.addElement(new Div("Item 2"));

 // Horizontal stack with centered items
 HStack hstack = new HStack();
 hstack.setGap(8).setAlign(AlignItems.CENTER);
 hstack.addElement(new Button("Save"));
 hstack.addElement(new Button("Cancel"));
 
Since:
2025
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • Stack

      protected Stack(FlexDirection direction)
      Constructs a Stack with the specified flex direction.
      Parameters:
      direction - the flex direction (ROW for horizontal, COLUMN for vertical)
  • Method Details

    • setGap

      public final T setGap(int gap)
      Sets the gap between child elements.
      Overrides:
      setGap in class StyledElement<T extends Stack<T>>
      Parameters:
      gap - the gap in pixels
      Returns:
      this Stack for method chaining
    • setGap

      public final T setGap(String gap)
      Sets the gap between child elements using a CSS value.
      Overrides:
      setGap in class StyledElement<T extends Stack<T>>
      Parameters:
      gap - the gap value (e.g., "1rem", "16px", "2em")
      Returns:
      this Stack for method chaining
    • setAlign

      public final T setAlign(AlignItems align)
      Sets the cross-axis alignment for child elements.

      For horizontal stacks (HStack), this controls vertical alignment. For vertical stacks (VStack), this controls horizontal alignment.

      Parameters:
      align - the alignment value
      Returns:
      this Stack for method chaining
    • setJustify

      public final T setJustify(JustifyContent justify)
      Sets the main-axis alignment for child elements.

      For horizontal stacks (HStack), this controls horizontal distribution. For vertical stacks (VStack), this controls vertical distribution.

      Parameters:
      justify - the justification value
      Returns:
      this Stack for method chaining
    • setWrap

      public final T setWrap(boolean wrap)
      Enables or disables wrapping of child elements.

      When enabled, children that overflow the container will wrap to the next line.

      Parameters:
      wrap - true to enable wrapping, false to prevent wrapping
      Returns:
      this Stack for method chaining
    • center

      public final T center()
      Centers all children both horizontally and vertically.

      This is a convenience method equivalent to calling:

      
       stack.setAlign(AlignItems.CENTER).setJustify(JustifyContent.CENTER);
       
      Returns:
      this Stack for method chaining
    • spaceBetween

      public final T spaceBetween()
      Distributes children with equal space between them.

      First and last children are positioned at the edges of the container, with equal space distributed between all children.

      Returns:
      this Stack for method chaining
    • spaceAround

      public final T spaceAround()
      Distributes children with equal space around them.

      Each child has equal space on both sides, resulting in half-size gaps at the edges.

      Returns:
      this Stack for method chaining
    • spaceEvenly

      public final T spaceEvenly()
      Distributes children with exactly equal space between them, including edges.
      Returns:
      this Stack for method chaining