Class Accordion


public class Accordion extends StyledElement<Accordion>
A container for vertically stacking expandable/collapsible panels.

Accordion provides a set of AccordionItem panels that can be expanded or collapsed. By default, only one panel can be open at a time (single mode), but this can be configured to allow multiple panels open simultaneously.

Features:

  • Single or multiple expansion modes
  • Optional borders between items
  • Fluent API for adding items

Usage:


 // Create accordion
 Accordion accordion = new Accordion();

 // Add items
 accordion.addItem(AccordionItem.create("Section 1", content1));
 accordion.addItem(AccordionItem.create("Section 2", content2));
 accordion.addItem(AccordionItem.create("Section 3", content3));

 // Allow multiple panels open
 accordion.setMultiple(true);

 // Expand first item by default
 accordion.getItem(0).expand();
 
Since:
2025
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • Accordion

      public Accordion()
      Constructs an empty Accordion.
  • Method Details

    • addItem

      public Accordion addItem(AccordionItem item)
      Adds an AccordionItem to the accordion.
      Parameters:
      item - the item to add
      Returns:
      this Accordion for method chaining
    • addItem

      public AccordionItem addItem(String headerText, Element<?> content)
      Creates and adds an AccordionItem with the specified header and content.
      Parameters:
      headerText - the header text
      content - the content element
      Returns:
      the created AccordionItem for further customization
    • getItem

      public AccordionItem getItem(int index)
      Returns the item at the specified index.
      Parameters:
      index - the item index
      Returns:
      the AccordionItem, or null if index is out of bounds
    • getItems

      public List<AccordionItem> getItems()
      Returns all items in the accordion.
      Returns:
      a list of AccordionItems
    • getItemCount

      public int getItemCount()
      Returns the number of items in the accordion.
      Returns:
      the item count
    • setMultiple

      public Accordion setMultiple(boolean multiple)
      Sets whether multiple items can be expanded simultaneously.

      When false (default), expanding an item collapses all others. When true, each item can be toggled independently.

      Parameters:
      multiple - true to allow multiple expanded items
      Returns:
      this Accordion for method chaining
    • isMultiple

      public boolean isMultiple()
      Checks if multiple expansion is enabled.
      Returns:
      true if multiple items can be expanded
    • expandAll

      public void expandAll()
      Expands all items.
    • collapseAll

      public void collapseAll()
      Collapses all items.
    • setItemBorder

      public Accordion setItemBorder(String border)
      Sets a border between items.
      Parameters:
      border - the border value (e.g., "1px solid #ddd")
      Returns:
      this Accordion for method chaining
    • setBorder

      public Accordion setBorder(String border)
      Adds a border around the entire accordion.
      Overrides:
      setBorder in class StyledElement<Accordion>
      Parameters:
      border - the border value (e.g., "1px solid #ddd")
      Returns:
      this Accordion for method chaining
    • setBorderRadius

      public Accordion setBorderRadius(int radius)
      Sets a border radius for the accordion container.
      Overrides:
      setBorderRadius in class StyledElement<Accordion>
      Parameters:
      radius - the border radius in pixels
      Returns:
      this Accordion for method chaining
    • setBorderRadius

      public Accordion setBorderRadius(String radius)
      Sets a border radius for the accordion container.
      Overrides:
      setBorderRadius in class StyledElement<Accordion>
      Parameters:
      radius - the border radius value (e.g., "8px", "0.5rem")
      Returns:
      this Accordion for method chaining
    • setGap

      public Accordion setGap(int gap)
      Sets a gap between accordion items.
      Overrides:
      setGap in class StyledElement<Accordion>
      Parameters:
      gap - the gap in pixels
      Returns:
      this Accordion for method chaining
    • setGap

      public Accordion setGap(String gap)
      Sets a gap between accordion items.
      Overrides:
      setGap in class StyledElement<Accordion>
      Parameters:
      gap - the gap value (e.g., "8px", "0.5rem")
      Returns:
      this Accordion for method chaining