Class ScrollBox


public class ScrollBox extends StyledElement<ScrollBox>
A scrollable container with configurable overflow behavior.

ScrollBox creates a container that scrolls when its content exceeds the container's dimensions. It supports vertical scrolling, horizontal scrolling, or both, with options for showing scrollbars always, only when needed, or hiding them.

Features:

  • Vertical, horizontal, or both-axis scrolling
  • Configurable scrollbar visibility
  • Fixed height/width constraints
  • Smooth scrolling option

Usage:


 // Vertical scrolling list with fixed height
 ScrollBox list = new ScrollBox();
 list.setHeight("400px");
 list.addElement(longList);

 // Horizontal scrolling gallery
 ScrollBox gallery = ScrollBox.horizontal();
 gallery.setHeight("200px");
 gallery.addElement(imageRow);

 // Both directions with max dimensions
 ScrollBox content = ScrollBox.both();
 content.setMaxHeight("80vh");
 content.setMaxWidth("100%");
 content.addElement(largeContent);

 // Smooth scrolling
 ScrollBox smooth = new ScrollBox().setSmooth();
 
Since:
2025
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • ScrollBox

      public ScrollBox()
      Constructs a vertical scrolling container.

      Content that exceeds the container's height will be scrollable. Horizontal overflow is hidden.

  • Method Details

    • horizontal

      public static ScrollBox horizontal()
      Creates a horizontal scrolling container.

      Content that exceeds the container's width will be scrollable. Vertical overflow is hidden.

      Returns:
      a new horizontal ScrollBox
    • both

      public static ScrollBox both()
      Creates a container that scrolls in both directions.

      Content that exceeds either dimension will be scrollable.

      Returns:
      a new bi-directional ScrollBox
    • setScrollY

      public ScrollBox setScrollY()
      Enables vertical scrolling.
      Returns:
      this ScrollBox for method chaining
    • setScrollX

      public ScrollBox setScrollX()
      Enables horizontal scrolling.
      Returns:
      this ScrollBox for method chaining
    • setHideY

      public ScrollBox setHideY()
      Disables vertical scrolling (hides overflow).
      Returns:
      this ScrollBox for method chaining
    • setHideX

      public ScrollBox setHideX()
      Disables horizontal scrolling (hides overflow).
      Returns:
      this ScrollBox for method chaining
    • setAlwaysShowY

      public ScrollBox setAlwaysShowY()
      Always shows the vertical scrollbar.
      Returns:
      this ScrollBox for method chaining
    • setAlwaysShowX

      public ScrollBox setAlwaysShowX()
      Always shows the horizontal scrollbar.
      Returns:
      this ScrollBox for method chaining
    • setSmooth

      public ScrollBox setSmooth()
      Enables smooth scrolling behavior.
      Returns:
      this ScrollBox for method chaining
    • setMaxHeight

      public ScrollBox setMaxHeight(String maxHeight)
      Sets the maximum height before scrolling begins.
      Overrides:
      setMaxHeight in class StyledElement<ScrollBox>
      Parameters:
      maxHeight - the maximum height (e.g., "400px", "50vh", "80%")
      Returns:
      this ScrollBox for method chaining
    • setMaxHeight

      public ScrollBox setMaxHeight(int maxHeight)
      Sets the maximum height before scrolling begins, in pixels.
      Overrides:
      setMaxHeight in class StyledElement<ScrollBox>
      Parameters:
      maxHeight - the maximum height in pixels
      Returns:
      this ScrollBox for method chaining
    • setMaxWidth

      public ScrollBox setMaxWidth(String maxWidth)
      Sets the maximum width before horizontal scrolling begins.
      Overrides:
      setMaxWidth in class StyledElement<ScrollBox>
      Parameters:
      maxWidth - the maximum width (e.g., "800px", "100%")
      Returns:
      this ScrollBox for method chaining
    • setMaxWidth

      public ScrollBox setMaxWidth(int maxWidth)
      Sets the maximum width before horizontal scrolling begins, in pixels.
      Overrides:
      setMaxWidth in class StyledElement<ScrollBox>
      Parameters:
      maxWidth - the maximum width in pixels
      Returns:
      this ScrollBox for method chaining
    • withPadding

      public ScrollBox withPadding(String padding)
      Adds padding inside the scroll container.
      Parameters:
      padding - the padding value (e.g., "1rem", "16px")
      Returns:
      this ScrollBox for method chaining
    • withPadding

      public ScrollBox withPadding(int padding)
      Adds padding inside the scroll container, in pixels.
      Parameters:
      padding - the padding in pixels
      Returns:
      this ScrollBox for method chaining