Class Carousel

All Implemented Interfaces:
Animation, Editable, StyleListener, Iterable<Component>

public class Carousel extends Container

A carousel (also called an image slider, view pager, banner rotator or onboarding pager): pages the user swipes between, with page dots under them and optional auto play.

Form form = new Form("Carousel", new BorderLayout());
Carousel carousel = new Carousel();
carousel.addPage(new Label("First"));
carousel.addPage(new Label("Second"));
carousel.addPage(new Label("Third"));
carousel.setAutoPlay(4000);
carousel.addSelectionListener((oldPage, newPage) ->
        System.out.println("Page " + (newPage + 1)));
form.add(BorderLayout.NORTH, carousel);
form.show();

It is swipeable Tabs with the tab bar hidden plus a PageIndicator, so it swipes, snaps and mirrors for right to left languages as Tabs does. #getTabs() and #getIndicator() give access to both for anything this class does not wrap.

Auto play advances one page per interval while the carousel is on screen, waits a full interval after the user touches it, and wraps from the last page to the first when #setLoop(boolean) is on (the default).

UIIDs: Carousel for the container, PageIndicator for the dots.

  • Constructor Details

    • Carousel

      public Carousel()
      An empty carousel; add pages with #addPage(Component).
  • Method Details

    • initUnselectedStyle

      protected void initUnselectedStyle(Style unselectedStyle)
      Description copied from class: Component

      Can be overridden by subclasses to perform initialization when the unselected style is set to a new value.

      Parameters
      • unselectedStyle: The unselected style.
      Overrides:
      initUnselectedStyle in class Component
    • addPage

      public Carousel addPage(Component page)

      Adds a page at the end.

      Parameters
      • page: the page's content
      Returns

      this, for chaining

    • removePage

      public void removePage(int index)

      Removes a page.

      Parameters
      • index: the zero based page
    • getPageCount

      public int getPageCount()

      The number of pages.

      Returns

      the page count

    • getSelectedIndex

      public int getSelectedIndex()

      The page on screen.

      Returns

      the zero based page

    • setSelectedIndex

      public void setSelectedIndex(int index, boolean animate)

      Moves to a page.

      Parameters
      • index: the zero based page

      • animate: true to slide there, false to jump

    • addSelectionListener

      public void addSelectionListener(SelectionListener l)

      Listens to page changes, from swipes, taps on the dots, auto play and #setSelectedIndex(int, boolean).

      Parameters
      • l: the listener, told the previous page and the new one
    • removeSelectionListener

      public void removeSelectionListener(SelectionListener l)

      Stops a listener from hearing page changes.

      Parameters
      • l: the listener
    • setAutoPlay

      public void setAutoPlay(int millis)

      Advances the pages on a timer.

      Parameters
      • millis: the time each page stays on screen; 0 or less turns auto play off
    • getAutoPlay

      public int getAutoPlay()

      The auto play interval.

      Returns

      milliseconds per page, 0 when auto play is off

    • setLoop

      public void setLoop(boolean loop)

      Whether auto play wraps from the last page to the first. On by default; off, auto play stops at the last page.

      Parameters
      • loop: true to wrap
    • isLoop

      public boolean isLoop()

      Whether auto play wraps from the last page to the first.

      Returns

      true if it wraps

    • setIndicatorPosition

      public void setIndicatorPosition(String position)

      Where the dots go: BorderLayout.SOUTH (the default), BorderLayout.NORTH, or null to hide them.

      Parameters
      • position: a BorderLayout constraint, or null
    • getTabs

      public Tabs getTabs()

      The swipeable tabs that hold the pages.

      Returns

      the tabs

    • getIndicator

      public PageIndicator getIndicator()

      The page dots.

      Returns

      the indicator

    • initComponent

      protected void initComponent()
      Description copied from class: Component
      Allows subclasses to bind functionality that relies on fully initialized and "ready for action" component state
      Overrides:
      initComponent in class Component
    • deinitialize

      protected void deinitialize()
      Description copied from class: Component
      Invoked to indicate that the component initialization is being reversed since the component was detached from the container hierarchy. This allows the component to deregister animators and cleanup after itself. This method is the opposite of the initComponent() method.
      Overrides:
      deinitialize in class Component