Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public class Carousel

  1. Object
  2. Component
  3. Container
  4. Carousel

ImplementsAnimation, Editable, Iterable<Component>, StyleListener

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.

Constructors

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

Methods

protected void initUnselectedStyle(Style unselectedStyle)Can be overridden by subclasses to perform initialization when the unselected style is set to a new value.
public Carousel addPage(Component page)Adds a page at the end.
public void removePage(int index)Removes a page.
public int getPageCount()The number of pages.
public int getSelectedIndex()The page on screen.
public void setSelectedIndex(int index, boolean animate)Moves to a page.
public void addSelectionListener(SelectionListener l)Listens to page changes, from swipes, taps on the dots, auto play and #setSelectedIndex(int, boolean).
public void removeSelectionListener(SelectionListener l)Stops a listener from hearing page changes.
public void setAutoPlay(int millis)Advances the pages on a timer.
public int getAutoPlay()The auto play interval.
public void setLoop(boolean loop)Whether auto play wraps from the last page to the first.
public boolean isLoop()Whether auto play wraps from the last page to the first.
public void setIndicatorPosition(String position)Where the dots go: BorderLayout.SOUTH (the default), BorderLayout.NORTH, or null to hide them.
public Tabs getTabs()The swipeable tabs that hold the pages.
public PageIndicator getIndicator()The page dots.
protected void initComponent()Allows subclasses to bind functionality that relies on fully initialized and “ready for action” component state
protected void deinitialize()Invoked to indicate that the component initialization is being reversed since the component was detached from the container hierarchy.

Inherited fields

Inherited methods

From Container

encloseIn, encloseIn, initLaf, getUIManager, setUIManager, isSurface, add, addAll, add, add, add, add, add, getLeadComponent, setLeadComponent, getLeadParent, keyPressed, keyReleased, getLayout, setLayout, invalidate, setShouldLayout, setShouldCalcPreferredSize, getLayoutWidth, getLayoutHeight, applyRTL, constrainWidthWhenScrollable, constrainHeightWhenScrollable, addComponent, addComponent, addComponent, addComponent, replaceAndWait, replaceAndWait, replace, replaceAndWait, replace, createReplaceTransition, isEnabled, setEnabled, removeComponent, cancelRepaints, flushReplace, removeAll, revalidateWithAnimationSafety, revalidate, revalidateLater, forceRevalidate, clearClientProperties, paint, paintGlass, layoutContainer, isSafeArea, setSafeArea, isSafeAreaRoot, getSafeAreaRoot, setSafeAreaRoot, getComponentCount, getComponentAt, getComponentIndex, contains, scrollComponentToVisible, getClosestComponentTo, getResponderAt, getComponentAt, findDropTargetAt, pointerPressed, calcPreferredSize, paramString, refreshTheme, isScrollableX, setScrollableX, isScrollableY, setScrollableY, getSideGap, getBottomGap, setScrollable, setCellRenderer, getScrollIncrement, setScrollIncrement, findFirstFocusable, dragInitiated, fireClicked, isSelectableInteraction, getGridPosY, paintComponentBackground, getGridPosX, animateHierarchyAndWait, createAnimateHierarchy, animateHierarchy, animateHierarchyFadeAndWait, createAnimateHierarchyFade, animateHierarchyFade, animateLayoutFadeAndWait, createAnimateLayoutFadeAndWait, animateLayoutFade, createAnimateLayoutFade, animateLayoutAndWait, animateLayout, updateTabIndices, createAnimateLayout, drop, createAnimateMotion, morph, morphAndWait, animateUnlayout, animateUnlayoutAndWait, createAnimateUnlayout, getChildrenAsList, iterator, iterator

From Component

setSameSize, isSetCursorSupported, parsePreferredSize, getDefaultDragTransparency, setDefaultDragTransparency, getEditingDelegate, setEditingDelegate, getCursor, setCursor, showNativeOverlay, hideNativeOverlay, updateNativeOverlay, getNativeOverlay, getAllStyles, getSameWidth, setSameWidth, getSameHeight, setSameHeight, getX, setX, getOuterX, getInnerX, getY, setY, getOuterY, getInnerY, isVisible, setVisible, getClientProperty, stripMarginAndPadding, putClientProperty, getDirtyRegion, setDirtyRegion, isOpaque, setOpaque, getWidth, setWidth, getOuterWidth, getInnerWidth, getHeight, setHeight, getOuterHeight, getInnerHeight, isDragRegion, getDragRegionStatus, getBaseline, getBaselineResizeBehavior, getPreferredSizeStr, setPreferredSizeStr, getPreferredSize, setPreferredSize, getScrollDimension, calcScrollSize, setScrollSize, getPreferredW, setPreferredW, getPreferredH, setPreferredH, getOuterPreferredH, getInnerPreferredH, getOuterPreferredW, getInnerPreferredW, setSize, getUIID, setUIID, setUIIDFinal, setUIID, getInlineAllStyles, setInlineAllStyles, getInlineSelectedStyles, setInlineSelectedStyles, getInlineUnselectedStyles, setInlineUnselectedStyles, getInlineDisabledStyles, setInlineDisabledStyles, getInlinePressedStyles, setInlinePressedStyles, remove, getParent, getOwner, setOwner, isOwnedBy, containsOrOwns, addFocusListener, removeFocusListener, addScrollListener, removeScrollListener, getSelectCommandText, setSelectCommandText, getLabelForComponent, setLabelForComponent, focusGained, focusLost, paintBackgrounds, paintShadows, getAbsoluteX, getAbsoluteY, isInClippingRegion, paintIntersectingComponentsAbove, paintScrollbars, paintScrollbarX, getScrollOpacity, getSelectedRect, paintScrollbarY, paintComponent, paintComponent, getBorder, getScrollable, paintBackground, isScrollable, getScrollX, setScrollX, getScrollY, setScrollY, onScrollX, onScrollY, getDraggedx, getDraggedy, contains, visibleBoundsContains, hasFixedPreferredSize, getBounds, getBounds, getVisibleBounds, getVisibleBounds, isFocusable, setFocusable, onSetFocusable, resetFocusable, getTabIndex, setTabIndex, getPreferredTabIndex, setPreferredTabIndex, isTraversable, setTraversable, handlesInput, setHandlesInput, consumesRawTextInput, hasFocus, setFocus, getComponentForm, getTopLevelContainer, repaint, repaint, longKeyPress, keyRepeated, registerForAnimation, deregisterFromAnimation, getAnimationManager, getScrollAnimationSpeed, setScrollAnimationSpeed, isBlockLead, setBlockLead, isIgnorePointerEvents, setIgnorePointerEvents, isRippleEffect, setRippleEffect, getInlineStylesTheme, setInlineStylesTheme, shouldRenderComponentSelection, isHideInLandscape, setHideInLandscape, createStyleAnimation, isSmoothScrolling, setSmoothScrolling, pointerHover, stopScrollMomentum, pointerHoverReleased, pointerHoverPressed, pinch, pinchReleased, pinch, rotation, isPinchBlocksDragAndDrop, setPinchBlocksDragAndDrop, pointerDragged, getDragImage, getDragTransparency, setDragTransparency, toImage, drawDraggedImage, draggingOver, dragEnter, dragExit, addPullToRefresh, setPullToRefresh, respondsToPointerEvents, pointerDragged, isStickyDrag, pointerPressed, isDragAndDropOperation, pointerReleased, longPointerPress, pointerReleased, setVerticalScrollBounds, setHorizontalScrollBounds, isVScrollThumbGrabbed, isHScrollThumbGrabbed, isVScrollThumbHover, isHScrollThumbHover, isTensileDragEnabled, setTensileDragEnabled, getTextSelectionSupport, addDropListener, removeDropListener, addDragOverListener, removeDragOverListener, isNativeDragSource, setNativeDragSource, getNativeDragOperation, setNativeDragOperation, createNativeDragOperation, isNativeDropTarget, setNativeDropTarget, getAcceptedDropMimeTypes, setAcceptedDropMimeTypes, getAcceptedDropActions, setAcceptedDropActions, canAcceptNativeDrop, nativeDragEnter, nativeDragOver, nativeDragExit, nativeDrop, addNativeDropListener, removeNativeDropListener, addNativeDragOverListener, removeNativeDragOverListener, dragFinished, addDragFinishedListener, addStateChangeListener, removeStateChangeListener, addPointerPressedListener, addLongPressListener, addContextMenuListener, removeContextMenuListener, addMouseWheelListener, removeMouseWheelListener, addStylusListener, removeStylusListener, getContextMenuCommands, setContextMenuCommands, mouseWheel, paintRippleOverlay, removePointerPressedListener, removeLongPressListener, removeDragFinishedListener, addPointerReleasedListener, removePointerReleasedListener, addPointerDraggedListener, removePointerDraggedListener, getDragSpeed, getStyle, getPressedStyle, getHoverStyle, setHoverStyle, isHovered, setHovered, setPressedStyle, initPressedStyle, initDisabledStyle, initSelectedStyle, getUnselectedStyle, setUnselectedStyle, getSelectedStyle, setSelectedStyle, getDisabledStyle, setDisabledStyle, installDefaultPainter, requestFocus, toString, refreshTheme, refreshTheme, isDragActivated, animate, scrollRectToVisible, scrollRectToVisible, paintBorder, paintBorderBackground, isCellRenderer, isScrollVisible, setScrollVisible, setIsScrollVisible, startEditingAsync, stopEditing, isEditing, isEditable, laidOut, isInitialized, setInitialized, styleChanged, getNextFocusDown, setNextFocusDown, getNextFocusUp, setNextFocusUp, getNextFocusLeft, setNextFocusLeft, getNextFocusRight, setNextFocusRight, getName, setName, initCustomStyle, deinitializeCustomStyle, isRTL, setRTL, isTactileTouch, isTactileTouch, setTactileTouch, getPropertyNames, getPropertyTypes, getPropertyTypeNames, getPropertyValue, setPropertyValue, paintLockRelease, paintLock, isSnapToGrid, setSnapToGrid, shouldBlockSideSwipe, shouldBlockSideSwipeLeft, shouldBlockSideSwipeRight, blocksSideSwipe, isFlatten, setFlatten, getTensileLength, setTensileLength, isGrabsPointerEvents, setGrabsPointerEvents, getScrollOpacityChangeSpeed, setScrollOpacityChangeSpeed, growShrink, isAlwaysTensile, setAlwaysTensile, isDraggable, setDraggable, isDropTarget, setDropTarget, isChildOf, isHideInPortrait, setHideInPortrait, getBindablePropertyNames, getBindablePropertyTypes, bindProperty, unbindProperty, getBoundPropertyValue, setBoundPropertyValue, getCloudBoundProperty, setCloudBoundProperty, getCloudDestinationProperty, setCloudDestinationProperty, getComponentState, setComponentState, setHidden, isHidden, setHidden, isHidden, announceForAccessibility, getAccessibilityText, setAccessibilityText, getSemantics, getAccessibilityNode, accessibilityChanged, accessibilityChanged, getTooltip, setTooltip

Constructor details

Carousel

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

Method details

initUnselectedStyle

protected void initUnselectedStyle(Style unselectedStyle)
Can be overridden by subclasses to perform initialization when the unselected style is set to a new value.

Parameters

unselectedStyle Style
The unselected style.

addPage

public Carousel addPage(Component page)
Adds a page at the end.

Parameters

page Component
the page’s content

Returns

this, for chaining

removePage

public void removePage(int index)
Removes a page.

Parameters

index int
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 int
the zero based page
animate boolean
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 SelectionListener
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 SelectionListener
the listener

setAutoPlay

public void setAutoPlay(int millis)
Advances the pages on a timer.

Parameters

millis int
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 boolean
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 String
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()
Allows subclasses to bind functionality that relies on fully initialized and “ready for action” component state

deinitialize

protected void deinitialize()
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.